convert series returned by pandas.Series.value_counts to a dictionary

后端 未结 2 2061
孤城傲影
孤城傲影 2020-12-10 11:15

I am trying to use pandas.Series.value_counts to get the frequency of values in a dataframe, so I go through each column and get values_count , which gives me a series:

2条回答
  •  误落风尘
    2020-12-10 11:44

    Extract keys and values for the dictionary from your_column and then zip it together.

    values = df['your_column'].value_counts(dropna=False).keys().tolist()
    counts = df['your_column'].value_counts(dropna=False).tolist()
    value_dict = dict(zip(values, counts))
    

提交回复
热议问题