Reputation: 23
I'm trying to merge two dataframes using pd.merge_asof. There is a 'Date2' in df2 related to 'Date1' in df1. However, there are some rows where there is no 'Date2'.
I tried the below code to merge both dataframes, but I'm getting the following error: ValueError: Merge keys contain null values on right side
df3 = pd.merge_asof(df1.sort_values('Date1', ascending=True),
df2.sort_values("Date2",ascending=True),
left_on='Date1',
right_on='Date2',
by=['Country','City','Location'],
direction='forward')
Could anyone please help me to figure out how to handle the null values on Date2.
Upvotes: 2
Views: 4514
Reputation: 2222
You can use .dropna() to remove the rows that have NaN values. Or use .fillna() to fill those rows with a default value .fillna(0)
or use .fillna(method='ffill')
to fill with the values from previous rows.
Upvotes: 2