简体   繁体   English

使用来自另一个数据帧中匹配索引的值设置数据帧列

[英]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 :我想使用保存在DF2col2匹配索引处的值在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 :您需要joinassign

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.

相关问题 Pandas:用于匹配行索引 - 使用不同列大小的其他 dataframe 的值更新 dataframe 值 - Pandas: for matching row indices - update dataframe values with values from other dataframe with a different column size 如果来自另一个 dataframe 的列和来自原始 dataframe 的列具有匹配值,则在原始 dataframe 中创建一个新列 - Create a new column in the original dataframe if the column from another dataframe and a column from original dataframe have matching values python:将 dataframe 中的值替换为特定索引处另一个 dataframe 的值 - python: replacing values in a dataframe with values from another dataframe at specific indices 基于匹配来自另一个数据帧pandas的值的新列 - New column based on matching values from another dataframe pandas 从另一个具有不同索引的 dataframe 在 pandas dataframe 添加新列 - Adding a new column in pandas dataframe from another dataframe with differing indices 根据熊猫索引将熊猫列从数据框合并到另一个数据框 - Merging pandas column from dataframe to another dataframe based on their indices 将Pandas DataFrame列值与另一个DataFrame列匹配 - Matching Pandas DataFrame Column Values with another DataFrame Column 有条件地使用另一个 Dataframe 中的值填充 Dataframe 中的列 - Filling a Column in a Dataframe conditionally using values from another Dataframe Gapfill pandas dataframe 在另一个 dataframe 中使用同一列中的值 - Gapfill pandas dataframe using values from same column in another dataframe 根据匹配另一个 dataframe 中的三个列值从 dataframe 中提取列 - Extracting column from dataframe based on matching three column values in another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM