Get count of values across columns-Pandas DataFrame

前端 未结 2 1934
有刺的猬
有刺的猬 2020-12-05 07:09

I have a Pandas DataFrame like following:

               A              B              C
0   192.168.2.85   192.168.2.85  124.43.113.22
1  192.248.8.183  192         


        
相关标签:
2条回答
  • 2020-12-05 07:59
    df['Counts'] = df[['col1','col2','col3']].groupby(['col1','col2','col3']).transform('count')
    
    0 讨论(0)
  • 2020-12-05 08:04

    stack it first and then use value_counts:

    In [14]: df.stack().value_counts()
    Out[14]: 
    192.248.8.183    3   
    192.168.2.85     3   
    66.249.74.52     2   
    192.168.2.161    2   
    124.43.113.22    1   
    dtype: int64
    
    0 讨论(0)
提交回复
热议问题