Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 495
s900n Avatar asked May 28 '17 20:05

s900n


People also ask

What does Value_counts () do in pandas?

Return a Series containing counts of unique values. The resulting object will be in descending order so that the first element is the most frequently-occurring element.

Can DataFrame be in a list?

There can be situations when you want to perform operations on a list instead of a pandas object. In such cases, you can store the DataFrame columns in a list and perform the required operations.

What is the difference between Value_counts and count?

count() should be used when you want to find the frequency of valid values present in columns with respect to specified col . . value_counts() should be used to find the frequencies of a series.

How can I get unique values from a column in pandas?

You can get unique values in column (multiple columns) from pandas DataFrame using unique() or Series. unique() functions. unique() from Series is used to get unique values from a single column and the other one is used to get from multiple columns.


1 Answers

Convert it to a dict:

bd = dict(b)
print(bd)
# {0.40000000000000002: 2, 0.5: 2, 0.59999999999999998: 1, 5.0: 1}

Don't worry about the long decimals. They're just a result of floating point representation; you still get what you expect from the dict.

bd[0.4]
# 2
like image 133
Arya McCarthy Avatar answered Nov 14 '22 22:11

Arya McCarthy