#πŸ”’ VLOOKUP using pandas

34 messages Β· Page 1 of 1 (latest)

safe temple
#

Is there any way to do vlookup function thing using pandas ?
I've got huge dataset(about 10 Million entries) onto which, I've to do vlookup.
Tried doing merge but did not get the output right

supple fulcrumBOT
#

@safe temple

Python help channel opened

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.

subtle hazel
#

generally speaking that's a merge, yeah

safe temple
#

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

safe temple
subtle hazel
#

Simplest way to fix that would be to drop duplicates by the key you're merging by.

safe temple
subtle hazel
#

df.drop_duplicates("some_column") leaves only the first row with a certain value of that column.

safe temple
subtle hazel
#

Sounds like that column is a string one - better convert it all to actual datetimes with pandas.to_datetime

safe temple
#

df[column]= pd.to_datetime(df[column], format="%d-%m-%Y %H:%M:%S")

rugged compass
#

!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)

supple fulcrumBOT
rugged compass
#

also don't use - unless you're using the ISO format

subtle hazel
#

I agree though, this format is quite cursed.

rugged compass
#

that tells pandas to infer the format of each row individually instead of assuming they all follow the same format

subtle hazel
#

"infer separately for each row".

safe temple
rugged compass
#
>>> pd.to_datetime(['10-30-2020', '30-10-2020'], format='mixed').strftime('%B %d %Y')
Index(['October 30 2020', 'October 30 2020'], dtype='object')
subtle hazel
#

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.

safe temple
#

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

safe temple
#

Thanks @subtle hazel @rugged compass 😁🫑πŸͺ

#

!close

supple fulcrumBOT
#
Python help channel closed

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.