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

半腔热情 提交于 2019-12-06 04:35:51

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
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

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

df.Count.groupby(df.Name.str.title()).sum().reset_index()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!