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

前端 未结 3 907
深忆病人
深忆病人 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 08:04

    You could use:

    df.groupby(['Col 1','Col 2']).size().unstack(fill_value=0).astype(bool)
    
    Col2      1     2      3
    Col1                    
    A     False  True   True
    B      True  True  False
    

提交回复
热议问题