Find value counts within a pandas dataframe of strings

前端 未结 4 1435

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:17

    Another solution using groupby and value_counts

    df.unstack().groupby(level=0).value_counts().unstack().T.fillna(0)
    Out[]:
                2017-08-09  2017-08-10
    active_1           2.0         3.0
    active_1-3         1.0         0.0
    active_3-7         1.0         1.0
    pre                1.0         1.0
    

    Or avoiding the last call to fillna

    df.unstack().groupby(level=0).value_counts().unstack(fill_value=0).T
    

提交回复
热议问题