Create a pandas dataframe of counts
Another way to do this, using value_counts
:
In [10]: df = pd.DataFrame({'Color': ['Red', 'Red', 'Blue'], 'State': ['MA', 'PA', 'PA']})
In [11]: df.Color.value_counts().reset_index().rename(
columns={'index': 'Color', 0: 'count'})
Out[11]:
Color count
0 Red 2
1 Blue 1
Essentially equivalent to setting the column names, but using the rename method instead:
df.groupby('Color').count().reset_index().rename(columns={'State': 'Count'})