Counting states in dataframe

Viewed 30

I have a data frame with string values of states and want to return how many values of each state. I want to use python and pandas to answer this question. I know its basic, but I appreciate the help.

the data frame is as follows;

States
California
Nevada
California
Washington
Washington
Washington

Which should return

California - 2 
Washington - 3
Nevada     - 1
1 Answers

This can be solved with value counts. First I made the data frame with:

df = pd.DataFrame({'States': ['California',
                              'Nevada',
                              'California',
                              'Washington',
                              'Washington',
                              'Washington']})

Then use the value counts function:

df['States'].value_counts()

That outputs:

enter image description here

Related