pandas value_counts applied to each column

前端 未结 6 971
甜味超标
甜味超标 2020-12-09 17:30

I have a dataframe with numerous columns (≈30) from an external source (csv file) but several of them have no value or always the same. Thus, I would to see qui

6条回答
  •  醉梦人生
    2020-12-09 17:49

    This is similar to @Jagie's reply but in addition:

    1. Put zero for values absent in a column
    2. Convert the counts to integer
        df = pd.DataFrame(
            data=[[34, 'null', 'mark'], [22, 'null', 'mark'], [34, 'null', 'mark']],     
            columns=["id", 'temp', 'name'], 
            index=[1, 2, 3]
        )
        result2 = df.apply(pd.value_counts).fillna(0).astype(int)
    

提交回复
热议问题