[英]Merging two Pandas DataFrames on specific condition
I have two Pandas dataframes, and I want to merge them on specific condition.我有两个 Pandas 数据框,我想在特定条件下合并它们。 These are my dataframes:这些是我的数据框:
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)
They look like this:它们看起来像这样:
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
I want from my result dataframe to look like this:我希望我的结果数据框看起来像这样:
food name color
0 fruit apple red
1 fruit apple green
2 fruit apple yellow
3 fruit grape white
4 fruit grape blue
So I want to merge them on "name" column but I want to drop nan values.所以我想将它们合并到“名称”列中,但我想删除 nan 值。 How can I do that?我怎样才能做到这一点?
You can use .merge
to join the dataframes, and the use .dropna
to drop the rows with NA values.您可以使用.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.