Count of elements in lists within pandas data frame

后端 未结 3 1072
你的背包
你的背包 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:56

    It is actually pretty easy with flattened lists and counters

    from matplotlib.cbook import flatten
    from collections import Counter
    
    din={'x':[['a','b','c'],['a','e','d', 'c']]}
    for a,i in din.items() :
        u=pd.DataFrame.from_dict(dict(Counter([*flatten(i)])), orient ='index').reset_index().rename(columns ={'index':a,0:str(a)+'_number'})
    

    output:

    However if din has several keys and values you will need a function to do the same trick

    from matplotlib.cbook import flatten
    from collections import Counter
    din={'x':[['a','b','c'],['a','e','d', 'c']], 'y': [['h','j'],['h','j','j']]}
    
    def foo(x):
        df = pd.DataFrame()
        for a,i in x.items() :
            u=pd.DataFrame.from_dict(dict(Counter([*flatten(i)])), orient ='index').reset_index().rename(columns ={'index':a,0:str(a)+'_number'})
            df=pd.concat([df,u])
        return df
    foo(din)
    

提交回复
热议问题