Combine Two Pandas Dataframe Into One New
I have two Pandas DataFrames whose data from different sources, but both DataFrames have the same column names. When combined only one column will keep the name. Like this: speed_d
Solution 1:
Use pd.merge
In [406]: (pd.merge(speed_df, temp_df, how='outer', on='ts')
.rename(columns={'val_x': 'speed','val_y': 'temp'})
.sort_values(by='ts'))
Out[406]:
ts speed temp
005.09.0114.08.042 NaN 7.0232.0 NaN
341.0 NaN
In [407]: (pd.merge(speed_df, temp_df, how='outer', on='ts')
.rename(columns={'val_x': 'speed', 'val_y': 'temp'})
.sort_values(by='ts').ffill())
Out[407]:
ts speed temp
005.09.0114.08.0424.07.0232.07.0341.07.0
Solution 2:
Two main DataFrame options, one is pd.merge
and the other is pd.fillna
. Here is the code:
df = speed_df.merge(temp_df, how='outer', on='ts')
df = df.rename(columns=dict(val_x='speed', val_y='temp'))
df = df.sort_values('ts')
df.fillna(method='ffill')
Hope this would be helpful.
Thanks
Post a Comment for "Combine Two Pandas Dataframe Into One New"