potato99
potato99

Reputation: 27

Get count of a value in a dataframe column and the value itself into separate lists

Let's say this is my frame:

   A
0  j
1  j
2  k
3  j
4  b
5  k

I am aware of value_counts() and .count() and groupby operations. My issue with those are that they return a series object / i can only really get the count itself out.

What I want to have happen is I want two separate lists (one with the value itself and the other the count) ordered in descending order of frequency like so:

ValueList=[j,k,b]
CountList=[3,2,1]

I am unsure of how to achieve this using pandas. Please help :(

Upvotes: 0

Views: 42

Answers (1)

jezrael
jezrael

Reputation: 862581

Convert Series index and values to lists:

s = df.A.value_counts()
ValueList = s.index.tolist()
CountList = s.tolist()
print (ValueList)
['j', 'k', 'b']

print (CountList)
[3, 2, 1]

Upvotes: 1

Related Questions