count the frequency that a value occurs in a dataframe column

后端 未结 13 1870
耶瑟儿~
耶瑟儿~ 2020-11-22 03:29

I have a dataset

|category|
cat a
cat b
cat a

I\'d like to be able to return something like (showing unique values and frequency)



        
13条回答
  •  渐次进展
    2020-11-22 04:07

    I believe this should work fine for any DataFrame columns list.

    def column_list(x):
        column_list_df = []
        for col_name in x.columns:
            y = col_name, len(x[col_name].unique())
            column_list_df.append(y)
    return pd.DataFrame(column_list_df)
    
    column_list_df.rename(columns={0: "Feature", 1: "Value_count"})
    

    The function "column_list" checks the columns names and then checks the uniqueness of each column values.

提交回复
热议问题