[英]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_id
和result.to_id
列的值,而不是使用 result.id 列的值, result.value.text
用result.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.