Home > Back-end >  How to find in Pandas which value counts n times?
How to find in Pandas which value counts n times?

Time:01-02

Say I have a Series like this

a = pd.Series([2, 4, 4, 2, 1, 4])  

I want to know how many times each value repeats in the series, than

a.value_counts()

provides

4    3
2    2
1    1

and particularly,

a.value_counts().max()

gives me the number

3

which is the number of times 4 is repeted.

My problem is that I don't find any line of code which provides me the number which is repeated 3 times, which is 4. Can you help me, please?

CodePudding user response:

Try with idxmax

a.value_counts().idxmax()

Or fix your code

a[a==a.value_counts().max()].index

CodePudding user response:

do this

a=a.value_counts()
a

output

4    3
2    2
1    1

then,

a.idxmax()

this gives 4 as output

this also works:

a.index[a.argmax()]
  • Related