Pandas: Transform dataframe to show if a combination of values exists in the orignal Dataframe

前端 未结 3 909
深忆病人
深忆病人 2021-01-13 07:55

I have a Dataframe that looks like this:

 | Col 1 | Col 2 | 
0|   A   |   2   |
1|   A   |   3   |
2|   B   |   1   |
3|   B   |   2   |

an

3条回答
  •  一生所求
    2021-01-13 07:55

    Here's a pivot solution:

    (df.pivot('Col 1', 'Col 2', 'Col 1').fillna(0) != 0).rename_axis(index=None, columns=None)
    
             1     2      3
    A      False  True   True
    B       True  True  False
    

提交回复
热议问题