#π VLOOKUP using pandas
34 messages Β· Page 1 of 1 (latest)
@safe temple
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
generally speaking that's a merge, yeah
the problem I'm facing is multiple values for a single column but I need the first occurence of the value that is being looked up
multiple occurences are creating multiple entries upon merge
Simplest way to fix that would be to drop duplicates by the key you're merging by.
thanks
will try that
how do I remove entire row, in case a value is duplicated in a particular column ?
df.drop_duplicates("some_column") leaves only the first row with a certain value of that column.
I'll sort the rows based on date-time column, but how do I make it consistent ?
some rows have :
dd-mm-yyyy while some have dd-mm-yyyy hh:mm
I want all to be on dd-mm-yyyy hh:mm
any way to do it ?
Sounds like that column is a string one - better convert it all to actual datetimes with pandas.to_datetime
tried that, got error
value error, format not matching
df[column]= pd.to_datetime(df[column], format="%d-%m-%Y %H:%M:%S")
!e I think you'll have to select based on the length, ```py
import pandas as pd
df = pd.DataFrame({'col': ['10-10-2010', '10-10-2010 10:10:10']})
short_format = df["col"].str.len() == len('dd-mm-yyyy')
df.loc[short_format, "col"] = pd.to_datetime(df.loc[short_format, "col"], format='%d-%m-%Y')
df.loc[~short_format, "col"] = pd.to_datetime(df.loc[~short_format, "col"], format='%d-%m-%Y %H:%M:%S')
df["col"] = df["col"].astype('datetime64[ns]')
print(df)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | col
002 | 0 2010-10-10 00:00:00
003 | 1 2010-10-10 10:10:10
also don't use - unless you're using the ISO format
For this example, it actually works to do pd.to_datetime(df["col"],format="mixed",dayfirst=True)
I agree though, this format is quite cursed.
what is mixed format ?
that tells pandas to infer the format of each row individually instead of assuming they all follow the same format
"infer separately for each row".
but does it convert them to consistent format ?
yes, but it is also very likely so give you wrong results like it'll parse 10-30-2020 and 30-10-2020 both into October 30 2020 without errors
>>> pd.to_datetime(['10-30-2020', '30-10-2020'], format='mixed').strftime('%B %d %Y')
Index(['October 30 2020', 'October 30 2020'], dtype='object')
Well, in your example it's the right behaviour, since 30 can't be a month.
And for the cases where it's ambigous - that's why I specify dayfirst=True.
that worked
now its the drop duplicates thats causing issue
Column A has multiple duplicates (Entire DF has 3 columns)
Keep First occurence and delete the entire row for next duplicates
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.