简体   繁体   English

熊猫列值从另一个数据框值更新

[英]pandas column value update from another dataframe value

I have following 2 dataframe 我有以下2个数据框

df_a = 
   id    val 
0  A100  11
1  A101  12
2  A102  13
3  A103  14
4  A104  15


df_b = 
   id    loc  val 
0  A100  12
1  A100  23
2  A100  32
3  A102  21
4  A102  38
5  A102  12
6  A102  18
7  A102  19
..... 

desired result: 预期结果:

df_b = 
   id    loc  val 
0  A100  12   11
1  A100  23   11 
2  A100  32   11
3  A102  21   12
4  A102  38   12 
5  A102  12   12
6  A102  18   12
7  A102  19   12 
..... 

When I try to update df_b's 'val' column by df_a's 'val' column like this, 当我尝试通过df_a的“ val”列更新df_b的“ val”列时,

for index, row in df_a.iterrows():
    v = row['val']
    seq = df_a.loc[df_a['val'] == v] 
    df_b.loc[df_b['val'] == v, 'val'] = seq['val'] 

or 要么

df_x = df_b.join(df_a, on=['id'], how='inner', lsuffix='_left', rsuffix='_right') 

However I could not solve this... How can I resolve this tricky things? 但是我无法解决这个问题...我该如何解决这个棘手的问题?

Thank you 谢谢

You can use map by Series created by set_index : 您可以使用map通过Series通过创建set_index

df_b['val'] = df_b['id'].map(df_a.set_index('id')['val'])
print (df_b)
     id  loc  val
0  A100   12   11
1  A100   23   11
2  A100   32   11
3  A102   21   13
4  A102   38   13
5  A102   12   13
6  A102   18   13
7  A102   19   13

Or merge with left join : 或与left join merge

df = pd.merge(df_b,df_a, on='id', how='left')

print (df)
     id  loc  val
0  A100   12   11
1  A100   23   11
2  A100   32   11
3  A102   21   13
4  A102   38   13
5  A102   12   13
6  A102   18   13
7  A102   19   13

If only one common column id used for joining in both df is possible omi it. 如果只有一个公共列id可以用于两个df连接,那么是不可能的。

df = pd.merge(df_b,df_a, how='left')
print (df)
     id  loc  val
0  A100   12   11
1  A100   23   11
2  A100   32   11
3  A102   21   13
4  A102   38   13
5  A102   12   13
6  A102   18   13
7  A102   19   13

暂无
暂无

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

相关问题 使用来自另一个具有条件的数据帧的值更新熊猫数据帧列 - update pandas dataframe column with value from another dataframe with condition 如何查找两个 pandas dataframe 并从另一个 dataframe 列更新第一个 dataframe 列中的值? - how to do lookup on two pandas dataframe and update its value in first dataframe column from another dataframe column? 从另一个数据帧中的列值替换pandas数据帧中的列中的值 - Replacing value in a column in pandas dataframe from a column value in another dataframe 从Pandas中不同数据框中的另一个匹配列更新数据框中的列值 - update a column value in a dataframe from another matching column in different dataframe in Pandas 仅当列没有值时,Pandas DataFrame 从另一个 dataframe 更新 - Pandas DataFrame update from another dataframe only if a column does not have a value Python Pandas 从另一个 dataframe 更新一个 dataframe 值 - Python Pandas update a dataframe value from another dataframe Pandas:将从 DataFrame 中提取的值乘以另一个 DataFrame 中的列值 - Pandas: Multiplying a value extracted from a DataFrame to column values in another DataFrame 如果另一个列中的值是另一个DataFrame中的pandas列? - pandas columns from another DataFrame if value is in another column? pandas-从另一个df元素的split元素更新数据框中的值 - pandas - update value in dataframe from split element of another df element 如何更新pandas数据框中另一列中特定值的行中的列值? - How to update a column value in a row for specific value in another column in pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM