Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:为特定列值的每个实例创建新列_Python_Pandas_Dataframe - Fatal编程技术网

Python:为特定列值的每个实例创建新列

Python:为特定列值的每个实例创建新列,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个这样的数据框 ---------------- 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

我有一个这样的数据框

----------------
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
或者


使用
set_index
groupby
cumcount

df.set_index(
    ['A', df.groupby('A').cumcount() + 1]
).B.unstack().add_prefix('B_').reset_index()

     A B_1 B_2   B_3
0  Cat   a   b  None
1  Dog   x   y     z
df.set_index(
    ['A', df.groupby('A').cumcount() + 1]
).B.unstack().add_prefix('B_').reset_index()

     A B_1 B_2   B_3
0  Cat   a   b  None
1  Dog   x   y     z