我有这个数据帧:如何创建新列以存储重复ID列的数据?
ID key
0 1 A
1 1 B
2 2 C
3 3 D
4 3 E
5 3 E
我想创造更多的key
列 - 作为necessary-到存储数据的时候有重复IDs
这是输出的一个片段的key
列:
ID key key2
0 1 A B # Note: ID#1 appeared twice in the dataframe, so the key value "B"
# associated with the duplicate ID will be stored in the new column "key2"
完整的输出应该像下面这样:
ID key key2 key3
0 1 A B NaN
1 2 C NaN NaN
2 3 D E E # The ID#3 has repeated three times. The key of
# of the second repeat "E" will be stored under the "key2" column
# and the third repeat "E" will be stored in the new column "key3"
任何建议或想法我应该如何解决这个问题?
感谢,
这是惊人的!是否有可能使代码处理相同的数据帧,但使用附加列“AltterKey”,因此数据帧总共有3列('ID','key'和'AlterKey')。我应该如何修改代码才能使其工作? @Alex – MEhsan
我的意思是如何将'lambda'函数应用到新列'AlterKey'? 谢谢,@Alex – MEhsan