Pyd
Pyd

Reputation: 6159

Removing duplicates with ignoring case sensitive and adding the next column values with the first one in pandas dataframe in python

I have a df,

Name    Count
Ram     1
ram     2
raM     1
Arjun   3
arjun   4

My desired output df,

Name    Count
Ram     4
Arjun   7

I tried groupby but I cannot achieve the desired output, please help

Upvotes: 2

Views: 7371

Answers (3)

piRSquared
piRSquared

Reputation: 294488

If I group by title formatted strings, it simplifies the steps I must take.

df.Count.groupby(df.Name.str.title()).sum().reset_index()

Upvotes: 3

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210882

In [71]: df.assign(Name=df['Name'].str.capitalize()).groupby('Name', as_index=False).sum()
Out[71]:
    Name  Count
0  Arjun      7
1    Ram      4

Upvotes: 3

jezrael
jezrael

Reputation: 863166

Use agg by values of Names converted to lower - first and sum:

df = (df.groupby(df['Name'].str.lower(), as_index=False, sort=False)
        .agg({'Name':'first', 'Count':'sum'}))
print (df)
    Name  Count
0    Ram      4
1  Arjun      7

Detail:

print (df['Name'].str.lower())
0      ram
1      ram
2      ram
3    arjun
4    arjun
Name: Name, dtype: object

Upvotes: 6

Related Questions