简体   繁体   English

当来自熊猫中另一个数据框的键匹配时更新数据框的列

[英]Update column of a dataframe when key matches from another dataframe in pandas

I have two dataframes.我有两个数据框。

在此处输入图像描述

For all rows in df1, find the corresponding row in df2 (through matching key) and update the final column in df2 to 1. How shall I proceed in pandas?对于 df1 中的所有行,找到 df2 中的相应行(通过匹配键)并将 df2 中的最后一列更新为 1。我应该如何在 pandas 中继续?

Remove column final , use left join with indicator parameter, so is possible create 1,0 column by mapping True, False by compare both :删除列final ,使用带indicator参数的左连接,因此可以通过比较both来映射True, False创建1,0列:

df = df2.drop('final', axis=1).merge(df1, how='left', indicator='final')
df['final'] = df['final'].eq('both').astype(int)

暂无
暂无

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

相关问题 Pandas 更新并添加第一行 dataframe 和另一个 dataframe 中的键列 - Pandas update and add rows one dataframe with key column in another dataframe 当 df1 中的键列与 df2 中的多个列匹配时,使用另一个数据框 (df2) 列中的值更新数据框 (df1) 列 - Update a dataframe(df1) column with value from another dataframe(df2) column when a key column in df1 matches to multiple columns in df2 如何根据另一个 DataFrame 中的列更新 Pandas DataFrame 中的列 - How to update a column in pandas DataFrame based on column from another DataFrame Append 如果某个列匹配,则 pandas 行包含来自另一行 dataframe 的数据 - Append a pandas row with data from another dataframe if a certain column matches 使用来自另一个具有条件的数据帧的值更新熊猫数据帧列 - update pandas dataframe column with value from another dataframe with condition pandas 在列值匹配时使用来自另一个数据帧的值更新数据帧 - pandas update a dataframe with values from another dataframe on the match of column values 当列值匹配时,Pandas Dataframe会从行中替换Nan - Pandas Dataframe replace Nan from a row when a column value matches 如果数据框中的另一列使用pandas匹配某个值,则从数据框中的列中减去值 - substract values from column in dataframe if another column in dataframe matches some value using pandas 如何查找两个 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 dataframe 列 - Update Pandas dataframe column from another frame using a conditional column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM