简体   繁体   English

Pandas:根据两个不同列的映射更改列中的值

[英]Pandas : change values in column based on a mapping of two different columns

I have this dataframe:我有这个数据框:

    id      result.value.text       result.value.labels result.id   result.from_id  result.to_id
0   793     skin melanoma           indication          5jSiC_n3IM  NaN              NaN
1   793     proteinase              protein             Lso-iCCHar  NaN              NaN
2   793     plasminogen activator   protein             _17D_kE5zf  NaN              NaN
3   793     NaN                     NaN                 NaN         5jSiC_n3IM       Lso-iCCHar
4   793     NaN                     NaN                 NaN         5jSiC_n3IM       _17D_kE5zf

I want to change the values of result.from_id and result.to_id columns, and instead of having the values of the result.id column, to replace them with the corresponding values of the result.value.text column.我想更改result.from_idresult.to_id列的值,而不是使用 result.id 列的值, result.value.textresult.id列的相应值替换它们。

Wanted Output想要的输出

    id      result.value.text       result.value.labels result.from_id  result.to_id
0   793     skin melanoma           indication          NaN                 NaN
1   793     proteinase              protein             NaN                 NaN
2   793     plasminogen activator   protein             NaN                 NaN
3   793     NaN                     NaN                 skin melanoma   proteinase
4   793     NaN                     NaN                 skin melanoma   plasminogen activator

Can someone help?有人可以帮忙吗?

Create dictionary with remove missing rows per result.id, result.value.text and then mapping both columns:创建字典,删除每个result.id, result.value.text中的缺失行,然后映射两列:

d = df.dropna(subset=['result.id','result.value.text']).set_index('result.id')['result.value.text'].to_dict()

cols = ['result.from_id','result.to_id']
df[cols] = df[cols].apply(lambda x: x.map(d))

暂无
暂无

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

相关问题 如何根据 pandas 中两个不同列的条件将值从一列复制到另一列? - how to copy values from one column into another column based on conditions of two different columns in pandas? 使用熊猫基于其他两列中的值替换列中的值 - Replace values in column based on values in two other columns using pandas 如何基于另一列的值(其中两个不同的列中的值在pandas中相等)找到一列的行索引? - How to find row index of one column based on values of a different column where the values in the two distinct columns are equal in pandas? Pandas:基于两个不同的列创建唯一值的索引 - Pandas: Creating an index of unique values based off of two different columns 如何根据不同列中的值向 pandas dataframe 添加一列? - How to add one column to pandas dataframe based on values in different columns? Pandas 根据 3 个不同列中的值添加带有 groupby 的新列 - Pandas add new column with groupby based on values in 3 different columns 根据Pandas中其他两个列的相等性从列中提取值 - Extract values from a column based on the equality of two other columns in Pandas 熊猫根据其他两个具有日期时间值的列创建一个布尔列 - pandas create a boolean column based on two other columns with datetime values 根据pandas中的第三列保留两列之间的值 - Keep values of between two columns based on third column in pandas Pandas:基于其他两列值的新列 - Pandas: New column based on values of two other columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM