Skip to content Skip to sidebar Skip to footer

Pandas: Add Column With The Most Recent Values

I have two pandas dataframes, both index with datetime entries. The df1 has non-unique time indices, whereas df2 has unique ones. I would like to add a column df2.a to df1 in the f

Solution 1:

You are describing an asof-join, which was just released in pandas 0.19.

pd.merge(df1, df2, left_on='ts', right_on='a')

Solution 2:

apply to rows of df1, reindex on df2 with ffill.

df1['df2.a'] = df1.apply(lambda x: pd.Series(df2.a.reindex([x.name]).ffill().values), axis=1)

Post a Comment for "Pandas: Add Column With The Most Recent Values"