[英]Merge two dataframes based on more than one overlap
I have two dataframes, each consisting of 3 columns with Year, Month and Day. 我有两个数据框,每个数据框由3列组成,分别是年,月和日。 df1 contains several other values that correspond to each date, and df2 contains different values. df1包含与每个日期对应的其他几个值,而df2包含不同的值。 I want to merge both of the dataframes into one where the year, month and day are all in common. 我想将两个数据框合并为一个年份,月份和日期都相同的数据框。 However, my first dataset goes from 1979-2017, but doesn't include all days or months 7-9. 但是,我的第一个数据集来自1979-2017,但不包括7-9的所有日期或月份。 The second dataframe goes from 1979-2017 and includes every single day. 第二个数据帧从1979-2017年开始,包括每一天。
df1: DF1:
Year Month Day IVT IWV Morevalues...
1979 1 10 275 21
1979 1 12 365 22
1979 1 16 565 29
df2: DF2:
Year Month Day NAO PNA
1979 1 9 1.2 0.76
1979 1 10 1.1 0.85
1979 1 11 1.15 0.82
1979 1 12 1.13 0.83
What I want: 我想要的是:
Year Month Day IVT IWV NAO PNA
1979 1 10 275 21 1.1 0.85
1979 1 12 365 22 1.13 0.83
尝试这个:
res = pd.merge(df1, df2, how='inner', on=['Year', 'Month', 'Day'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.