Find value counts within a pandas dataframe of strings

前端 未结 4 1431

I want to get the frequency count of strings within a column. One one hand, this is similar to collapsing a dataframe to a set of rows that only reflects the strings in the

4条回答
  •  醉话见心
    2020-12-21 14:08

    I do not know why I addict to using apply in this strange way ...

    df.apply(lambda x : x.groupby(x).count()).fillna(0)
    Out[31]: 
                2017-08-09  2017-08-10
    active_1             2         3.0
    active_1-3           1         0.0
    active_3-7           1         1.0
    pre                  1         1.0
    

    Or

    import collections
    df.apply(lambda x : pd.Series(collections.Counter(x))).fillna(0)
    

    As what I expected simple for loop is faster than apply

    pd.concat([pd.Series(collections.Counter(df[x])) for x in df.columns],axis=1)
    

提交回复
热议问题