小编mub*_*007的帖子

如何为 Pandas 中数据帧的给定列中的每个唯一组件创建一个新列?

我对 Pandas 比较陌生,所以如果问题的框架不正确,我深表歉意。我有以下数据框

df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar',
                         'foo', 'bar', 'foo', 'foo'],
                   'B': ['one', 'one', 'two', 'three',
                         'two', 'two', 'one', 'three'],
                   'C': np.random.randn(8)})



     A      B         C         
0  foo    one  0.469112 
1  bar    one -0.282863 
2  foo    two -1.509059
3  bar  three -1.135632  
4  foo    two  1.212112  
5  bar    two -0.173215 
6  foo    one  0.119209 
7  foo  three -1.044236 
Run Code Online (Sandbox Code Playgroud)

我想要实现的是以下,

           foo_B         foo_C      bar_B      bar_C          
0             one        0.469112     -           -
1             -            -          one        -0.282863 
2             two        -1.509059    -            - …
Run Code Online (Sandbox Code Playgroud)

python python-3.x pandas

6
推荐指数
1
解决办法
47
查看次数

标签 统计

pandas ×1

python ×1

python-3.x ×1