简体   繁体   English

Pandas 用来自其他 dataframe 的字符串替换值

[英]Pandas replacing value with string from other dataframe

i want to replace all cell values(int)[dataset1] with the name(str)[dataset2] where the index of [dataset2] is matching with the values from [dataset1].我想用名称(str)[dataset2]替换所有单元格值(int)[dataset1],其中[dataset2]的索引与[dataset1]中的值匹配。

enter image description here在此处输入图像描述

It looks so simple... but I'm completely stuck.它看起来很简单......但我完全被卡住了。

merge() the names dataframe onto each of the value columns then cleanup.将名称 dataframe merge()到每个值列,然后清理。

df1 = pd.DataFrame({"id":[1111,1122,1133,1144,1155,1166],
              "name":["red","blue","green","yellow","magenta","black"]})
df2 = pd.DataFrame({"value1":[1122,1144,1166,1111,1111,1155],
             "value2":[np.nan,np.nan,1133,np.nan,1144,np.nan]})

output = (df2
 .merge(df1, left_on="value1", right_on="id", how="left")
 .rename(columns={"name":"name1"})
 .merge(df1, left_on="value2", right_on="id", how="left")
 .rename(columns={"name":"name2"})
 .drop(columns=["id_x","id_y","value1","value2"])
)

print(output.to_string())

output output

     name1   name2
0     blue     NaN
1   yellow     NaN
2    black   green
3      red     NaN
4      red  yellow
5  magenta     NaN

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM