如何使用日期列将这两个数据框合并在一起,而不必重复很多行:
data = {'date':['01/01/2018', '02/02/2019', '01/04/2019', '16/02/2019','20/03/2019'], 'Age':[20, 21, 19, 18,34]}
# Create DataFrame
df1 = pd.DataFrame(data)
df1
DF2
data2 = {'date':['01/01/2018', '04/07/2019', '01/04/2019', '18/02/2018'], 'miles':[50, 81, 99, 109]}
# Create DataFrame
df2 = pd.DataFrame(data2)
df2
最终结果应如下所示:
finaldata = {'date':['01/01/2018', '02/02/2019', '01/04/2019', '16/02/2019','20/03/2019'], 'Age':[20, 21, 19, 18,34], 'miles':[50, 'NAN', 99, 'NAN', 'NAN']}
# Create DataFrame
final_df = pd.DataFrame(finaldata)
final_df
我已经在我的数据集上尝试过此代码,但是它创建了很多重复的行
df1.merge(df2)
答案 0 :(得分:2)
使用“如何”参数:
df1.merge(df2, how='left')
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html