Home > Mobile >  How to group multiple columns while replacing zero with values (pandas)?
How to group multiple columns while replacing zero with values (pandas)?

Time:11-27

Name Cat Dog Frog Pig
Ana 0 1 0 0
Ana 1 0 1 0
Name Cat Dog Frog Pig
Ana 1 1 1 0

I'd like to group these two rows by name and replace the 'zeros' by one when is filled. The output should be like this

CodePudding user response:

Use groupby with max()

df = df.groupby('Name').max().reset_index()

output:

> df

  Name  Cat  Dog  Frog  Pig
0  Ana    1    1     1    0

CodePudding user response:

what you might want to do here is an aggregation. One way to obtain your desired output is to use the pandas dataframe methods grouby() and sum()

Here is how I would do it.

import pandas as pd

data = [
        ('Ana', 0, 1, 0, 0)
,       ('Ana', 1, 0, 1, 0)        
]

df = pd.DataFrame(data, columns=['Name', 'Cat', 'Dog', 'Frog', 'Pig'])

print(df.groupby(['Name']).sum())

Then the output would be:

      Cat  Dog  Frog  Pig
Name
Ana     1    1     1    0

If you want to know more about these methods, you can follow the links below:

groupby(): https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html

sum(): https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sum.html

  • Related