Count of elements in lists within pandas data frame

后端 未结 3 1073
你的背包
你的背包 2021-02-04 05:13

I need to get the frequency of each element in a list when the list is in a pandas data frame columns

In data:

din=pd.DataFrame({\'x\':[[\'a\',\'b\',\'c         


        
3条回答
  •  滥情空心
    2021-02-04 05:57

    First flatten values of lists and then count by value_counts or size or Counter:

    a = pd.Series([item for sublist in din.x for item in sublist])
    

    Or:

    a = pd.Series(np.concatenate(din.x))
    

    df = a.value_counts().sort_index().rename_axis('x').reset_index(name='f')
    

    Or:

    df = a.groupby(a).size().rename_axis('x').reset_index(name='f')
    

    from collections import Counter
    from  itertools import chain
    
    df = pd.Series(Counter(chain(*din.x))).sort_index().rename_axis('x').reset_index(name='f')
    
    print (df)
       x  f
    0  a  2
    1  b  1
    2  c  2
    3  d  1
    4  e  1
    

提交回复
热议问题