需要帮助过滤和合并两个熊猫数据框

时间:2020-10-03 21:33:43

标签: python pandas

嗨,我有这两个熊猫数据框

                                                     NAME                GEO_ID     RR2010  STATE  COUNTY   TRACT
0             Census Tract 9508, Genesee County, New York  1400000US36037950800      67.9     36      37  950800
1          Census Tract 9505.02, Genesee County, New York  1400000US36037950502      74.0     36      37  950502
2             Census Tract 9506, Genesee County, New York  1400000US36037950600      75.3     36      37  950600
3             Census Tract 9507, Genesee County, New York  1400000US36037950700      63.4     36      37  950700
4             Census Tract 9509, Genesee County, New York  1400000US36037950900      74.2     36      37  950900
5             Census Tract 9510, Genesee County, New York  1400000US36037951000      68.9     36      37  951000
6             Census Tract 9511, Genesee County, New York  1400000US36037951100      72.8     36      37  951100
7             Census Tract 9512, Genesee County, New York  1400000US36037951200      72.7     36      37  951200
8             Census Tract 9513, Genesee County, New York  1400000US36037951300      75.1     36      37  951300
9             Census Tract 9514, Genesee County, New York  1400000US36037951400      71.0     36      37  951400
10              Census Tract 801, Greene County, New York  1400000US36039080100      74.6     36      39   80100
                                                     NAME                GEO_ID               RR2020  STATE  COUNTY   TRACT
0          Census Tract 9505.02, Genesee County, New York  1400000US36037950502                 74.2     36      37  950502
1             Census Tract 9506, Genesee County, New York  1400000US36037950600                 68.4     36      37  950600
2             Census Tract 9507, Genesee County, New York  1400000US36037950700                 53.7     36      37  950700
3             Census Tract 9508, Genesee County, New York  1400000US36037950800                 60.6     36      37  950800
4             Census Tract 9509, Genesee County, New York  1400000US36037950900                 66.4     36      37  950900
5             Census Tract 9510, Genesee County, New York  1400000US36037951000                 60.1     36      37  951000
6             Census Tract 9511, Genesee County, New York  1400000US36037951100                 63.6     36      37  951100
7             Census Tract 9512, Genesee County, New York  1400000US36037951200                 62.8     36      37  951200
8             Census Tract 9513, Genesee County, New York  1400000US36037951300                 68.8     36      37  951300
9             Census Tract 9514, Genesee County, New York  1400000US36037951400                 70.1     36      37  951400
10              Census Tract 801, Greene County, New York  1400000US36039080100                 50.0     36      39   80100

但是我要做的是将这两个DF的TRACT编号进行比较。如果这两个df的TRACT编号不同,则我想删除与其关联的整个行。 另外,我想提取两年的RR,并将它们减去并作为新的Column添加到两个DF中,我该怎么做?

1 个答案:

答案 0 :(得分:0)

# merging dataframes
df3 = df1.merge(df2, on='TRACT', how='inner')

# adding new column
df3['DIFFERENCE'] = df3['RR2020'] - df3['RR2010']
相关问题