Check value of two different length columns in different dataframes python

Viewed 30

I have a question regarding comparing two columns of different lengths from different data frames. I have two data sets that are between the same dates but just separated by time measurements. For example one Dataframe df_daily has a column Date where the date is a str ('2/1/2022') as an example. The other one is df_minute where it has data on minutes but also has a column named Date with the same properties.

I have tried to check with a nested for loop however the data frames are larger and it takes for ever to compile the output.

The length for df_minute is 145966 and df_daily is 157.

Below is how the nested for loops look. The goal for this is to append the value from the df_daily to the index location where the dates are the same in df_minute. I want to append the value at column Sales.

salesList=[]
for i in range(len(df_minute)):
dateMinute = df_minute.iloc[i]["Date"]

for j in range(len(df_daily)):
    dateDaily = df_daily.iloc[j]['Date']
if dateMinute == dateDaily:
    salesList.append(df_daily.iloc[i]['Sales'])

df_minute['Daily Sales'] = salesList

Is there a faster and more efficient way of doing this. I think this can be done with V look up in excel but is there a way to this better with python?

Let me know if there is anything I can do to make this more clear.

1 Answers

You didn't provide any data, but vlookup in pandas should be something like this:

df_minute.merge(df_daily, on='Date')

If df_daily has more columns you don't need to merge everything:

df_minute.merge(df_daily[['Date', 'Sales']], on='Date')
Related