pandas 如何从另一个 Dataframe 的数据不匹配的 Dataframe 中删除行

igsr9ssn  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(139)

我有两个 Dataframe :

gp_df = CCA3            Country/Territory  year       GDP_USD
2662   AFG                  Afghanistan  1970  1.748887e+09
2661   AFE  Africa Eastern and Southern  1970  4.486261e+10
2663   AFW   Africa Western and Central  1970  2.350461e+10
2665   ALB                      Albania  1970           NaN
2720   DZA                      Algeria  1970  4.863487e+09
...    ...                          ...   ...           ...
16156  PSE           West Bank and Gaza  2020  1.553170e+10
16219  WLD                        World  2020  8.490680e+13
16222  YEM                  Yemen, Rep.  2020  1.884051e+10
16224  ZMB                       Zambia  2020  1.811063e+10
16225  ZWE                     Zimbabwe  2020  1.805117e+10

pp_df = CCA3  Country/Territory Continent  2020 Population  1970 Population 
0    AFG        Afghanistan      Asia         38972230         10752971   
1    ALB            Albania    Europe          2866849          2324731   
2    DZA            Algeria    Africa         43451666         13795915   
3    ASM     American Samoa   Oceania            46189            27075   
4    AND            Andorra    Europe            77700            19860   
..   ...                ...       ...              ...              ...   
229  WLF  Wallis and Futuna   Oceania            11655             9377   
230  ESH     Western Sahara    Africa           556048            76371   
231  YEM              Yemen      Asia         32284046          6843607   
232  ZMB             Zambia    Africa         18927715          4281671   
233  ZWE           Zimbabwe    Africa         15669666          5202918

我想找到一种方法从gp_df中删除所有不在pp_df中的Countrys/Territorys。我已经尝试使用.drop()和np.where()来尝试定位重复的内容,然后删除它们,但我似乎无法获得正确的语法。

huwehgph

huwehgph1#

这个有用吗...

gp_df[~gp_df['Country/Territory'].isin(pp_df['Country/Territory'])]

相关问题