0

I have a dataframe:

import pandas as pd
df = pd.DataFrame({'letter':   ['A','A','B','B','C','C'],
                    'value':[  1,  2,  3,  3,  7,  5],
                    'state':['CA','WA','WA','WA','CA','NV']})

enter image description here

Count how many times a value appears in the state column:

df['state'].value_counts()

enter image description here

This is a pandas.core.series.Series (according to type(df['state'].value_counts()). So:

df['state'].value_counts()[0]

Only returns 3, not WA 3.

How do I get the name of the entries value_counts() counts? ie how do I get WA?


I intend this as a self-answered question, if there are better answers, they are welcome.

zabop
  • 6,750
  • 3
  • 39
  • 84

2 Answers2

1

Add []

df['state'].value_counts()[[0]] # if would like return the index, add .index
WA    3
Name: state, dtype: int64
BENY
  • 317,841
  • 20
  • 164
  • 234
0

Can get the indeces of the pandas.core.series.Series, ie of df['state'].value_counts() object by:

df['state'].value_counts().index

which returns:

Index(['WA', 'CA', 'NV'], dtype='object')

of datatype pandas.core.indexes.base.Index. Form a list:

list(df['state'].value_counts().index)

giving:

['WA', 'CA', 'NV']
zabop
  • 6,750
  • 3
  • 39
  • 84