Python Pandas: Get dataframe.value_counts() result as list

Viewed 13551

I have a DataFrame and I want to get both group names and corresponding group counts as a list or numpy array. However when I convert the output to matrix I only get group counts I dont get the names. Like in the example below:

  df = pd.DataFrame({'a':[0.5, 0.4, 5 , 0.4, 0.5, 0.6 ]})
  b = df['a'].value_counts()
  print(b)

output:

[0.4    2
0.5    2
0.6    1
5.0    1
Name: a, dtype: int64]

what I tried is print[b.as_matrix()]. Output:

[array([2, 2, 1, 1])]

In this case I do not have the information of corresponding group names which also I need. Thank you.

3 Answers

most simplest way

list(df['a'].value_counts())
Related