我有一个像这样的数据框
----------------
RecID| A |B
----------------
1 |Dog | x
2 |Dog | y
3 |Dog | z
4 |Cat | a
5 |Cat | b
并且想知道是否有办法将其转换为这样:
-----------------------------
RecID| A |B_1|B_2|B_3|
-----------------------------
1 |Dog| x | y | z |
2 |Cat| a | b | NA|
基本上,为 B 的每个可能值创建新列,并按 A 的特定值分组,并在需要时填充 NA。
最佳答案
一种方法是
In [294]: (df.groupby('A', sort=False).B.apply(list)
.apply(pd.Series).add_prefix('B_').reset_index())
Out[294]:
A B_0 B_1 B_2
0 Dog x y z
1 Cat a b NaN
或者,
In [320]: (df.groupby('A', sort=False).B.apply(lambda x: pd.Series(x.values))
.unstack().rename(columns=lambda x: 'B_{}'.format(int(x)+1))
.reset_index())
Out[320]:
A B_1 B_2 B_3
0 Dog x y z
1 Cat a b None
关于Python Pandas : Create New Columns For Each Instance of A Particular Column Value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45967900/