繁体   English   中英

在特定条件下合并两个 Pandas DataFrame

[英]Merging two Pandas DataFrames on specific condition

我有两个 Pandas 数据框,我想在特定条件下合并它们。 这些是我的数据框:

import pandas as pd

pd.set_option('display.max_rows', 250)
pd.set_option('display.max_columns', 7)
pd.set_option('display.width', 800)


df1 = pd.DataFrame({"food":["fruit", "fruit", "fruit"],
                    "name":["apple", "grape", "bannana"]})
print(df1)


df2 = pd.DataFrame({"name":["apple", "apple", "apple", "grape", "grape"],
                    "color":["red", "green", "yellow","white", "blue"]})
print(df2)

它们看起来像这样:

    food     name
0  fruit    apple
1  fruit    grape
2  fruit  bannana

    name   color
0  apple     red
1  apple   green
2  apple  yellow
3  grape   white
4  grape    blue

我希望我的结果数据框看起来像这样:

    food   name   color
0  fruit  apple     red
1  fruit  apple   green
2  fruit  apple  yellow
3  fruit  grape   white
4  fruit  grape    blue

所以我想将它们合并到“名称”列中,但我想删除 nan 值。 我怎样才能做到这一点?

您可以使用.merge加入数据框,并使用.dropna删除具有 NA 值的行。

df1.merge(df2, how='left', on='name').dropna()
# returns:
    food   name   color
0  fruit  apple     red
1  fruit  apple   green
2  fruit  apple  yellow
3  fruit  grape   white
4  fruit  grape    blue

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM