TheCrystalShip
TheCrystalShip

Reputation: 259

pandas value_counts include all values before groupby

Lets say I have the following dataframe:

df = pd.DataFrame([['a',1, -1], ['a', 1, -1], ['b', 0, -1], ['c', -1, -1]] ,columns = ['col1', 'col2', 'col3'])
df
    col1    col2    col3
0   a       1       -1
1   a       1       -1
2   b       0       -1
3   c       -1      -1

Now I want to groupby the df by the columns, and for each one, count the number of appearances of the values in the columns col1 separately.

groupby_df = df.groupby('col1') 
for a,b in groupby_df:
    print("{0} -> \n{1}".format(a, b['col1'].value_counts().sort_index()))

I get:

a -> 
a    2
Name: col1, dtype: int64
b -> 
b    1
Name: col1, dtype: int64
c -> 
c    1
Name: col1, dtype: int64

But I want to count the number of appearances separately , and still include all the column values, as follows:

a -> 
a    2
b    0
c    0
Name: col1, dtype: int64
b -> 
a    0
b    1
c    0
Name: col1, dtype: int64
c -> 
a    0
b    0
c    1
Name: col1, dtype: int64

Any help will be appreciated!

Upvotes: 1

Views: 318

Answers (1)

smj
smj

Reputation: 1284

Try using .reindex():

import pandas as pd

df = pd.DataFrame([['a',1, -1], ['a', 1, -1], ['b', 0, -1], ['c', -1, -1]] ,columns = ['col1', 'col2', 'col3'])

# Create index using unique values of col1.

uniques = pd.Index(df['col1'].unique())

# Group.

groupby_df = df.groupby('col1')

# Use reindex to assign and autoamtically align the value counts with the index.

for a, b in groupby_df:
    print(b['col1'].value_counts().sort_index().reindex(uniques, fill_value = 0))

Gives:

a    2
b    0
c    0
Name: col1, dtype: int64
a    0
b    1
c    0
Name: col1, dtype: int64
a    0
b    0
c    1
Name: col1, dtype: int64

Upvotes: 1

Related Questions