pandas 熊猫合并的重复列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40343061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Duplicate columns with Pandas merge?
提问by user1566200
I have a data frame a:
我有一个数据框a:
ID value1
1 nan
2 nan
3 nan
4 nan
5 nan
and then two other data frames, band c:
然后是另外两个数据框,b以及c:
ID value1
2 20
3 10
ID value1
1 58
4 20
When I do a.merge(b, on='ID').merge(c, on='ID'), I get duplicate columns of value. My end result has the columns:
当我这样做时a.merge(b, on='ID').merge(c, on='ID'),我会得到重复的value. 我的最终结果有以下列:
ID value1_x value1_y value1
but I want to end up with:
但我想结束:
ID value1
1 58
2 20
3 10
4 20
5 nan
How do I use band cto populate the values in awithout duplicate columns?
如何在没有重复列的情况下使用b和c填充值a?
回答by jezrael
You can use concatand then mergewith dropold column value1in a:
您可以使用concat,然后merge在drop旧列value1中使用a:
df1 = pd.concat([b,c])
print (df1)
ID value1
0 2 20
1 3 10
0 1 58
1 4 20
df2 = pd.merge(a ,df1, on='ID', how='left', suffixes=('_',''))
df2.drop('value1_', axis=1, inplace=True)
print (df2)
ID value1
0 1 58.0
1 2 20.0
2 3 10.0
3 4 20.0
4 5 NaN

