我有一个pandas数据框:
+--------+---------+------+----------------+
| Name | Address | ID | Linked_To |
+--------+---------+------+----------------+
| Name A | ABC | 1233 | 1234;1235 |
| Name B | DEF | 1234 | 1233;1236;1237 |
| Name C | GHI | 1235 | 1234;1233;2589 |
+--------+---------+------+----------------+
Linked_To列中的某些ID是Name列下的记录。我可以创建一个字典并将Linked_To列中的数据作为列表传递。但是,我不确定如何继续。理想情况下,我希望看到类似的内容:
+--------+---------+------+-------------------------+
| Name | Address | Id | Linked To |
+--------+---------+------+-------------------------+
| Name A | ABC | 1233 | Name B;Name C |
| Name B | DEF | 1234 | Name A;Name D; Name E |
| Name C | HIJ | 1235 | Name B;Name A; None |
+--------+---------+------+-------------------------+
答案 0 :(得分:1)
没有一些循环似乎很难做到这一点:
linked = df.Linked_To.str.split(';')
def pull_name(iden):
try:
return df[df.ID == int(iden)].Name.iat[0]
except:
return str(None)
res = linked.apply(lambda ids: '; '.join([pull_name(i) for i in ids]))
print(res)
0 Name B; Name C
1 Name A; None; None
2 Name B; Name A; ...
Name: Linked_To, dtype: object