[英]Set dataframe column using values from matching indices in another dataframe
I would like to set values in col2
of DF1
using the value held at the matching index of col2
in DF2
:我想使用保存在
DF2
中col2
匹配索引处的值在DF1
col2
中设置值:
DF1
: DF1
:
col1 col2
index
0 a
1 b
2 c
3 d
4 e
5 f
DF2
: DF2
:
col1 col2
index
2 a x
3 d y
5 f z
DF3
: DF3
:
col1 col2
index
0 a NaN
1 b NaN
2 c x
3 d y
4 e NaN
5 f z
If I just try and set DF1['col2'] = DF2['col2']
then col2
comes out as all NaN
values in DF3
- I take it this is because the indices are different.如果我只是尝试设置
DF1['col2'] = DF2['col2']
然后col2
作为DF3
所有NaN
值出现 - 我认为这是因为索引不同。 However when I try and use map()
to do something like:但是,当我尝试使用
map()
执行以下操作时:
DF1.index.to_series().map(DF2['col2'])
then I still get the same NaN
column, but I thought it would map the values over where the index matches...然后我仍然得到相同的
NaN
列,但我认为它会将值映射到索引匹配的位置...
What am I not getting?我没有得到什么?
You need join
or assign
:您需要
join
或assign
:
df = df1.join(df2['col2'])
print (df)
col1 col2
index
0 a NaN
1 b NaN
2 c x
3 d y
4 e NaN
5 f z
Or:或者:
df1 = df1.assign(col2=df2['col2'])
#same like
#df1['col2'] = df2['col2']
print (df1)
col1 col2
index
0 a NaN
1 b NaN
2 c x
3 d y
4 e NaN
5 f z
If no match and all values are NaN
s check if indices have same dtype in both df
:如果不匹配且所有值都是
NaN
请检查索引在两个df
是否具有相同的数据类型:
print (df1.index.dtype)
print (df2.index.dtype)
If not, then use astype:如果没有,则使用 astype:
df1.index = df1.index.astype(int)
df2.index = df2.index.astype(int)
Bad solution (check index 2):错误的解决方案(检查索引 2):
df = df2.combine_first(df1)
print (df)
col1 col2
index
0 a NaN
1 b NaN
2 a x
3 d y
4 e NaN
5 f z
You can simply concat as you are combining based on index您可以在根据索引进行组合时简单地进行连接
df = pd.concat([df1['col1'], df2['col2']],axis = 1)
col1 col2
index
0 a NaN
1 b NaN
2 c x
3 d y
4 e NaN
5 f z
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.