Home > other >  Merge rows in pandas dataframe and sum them
Merge rows in pandas dataframe and sum them

Time:08-07

suppose I have a dataframe as below:

Name Date
First some date
first some date
FIRST some date
First some date

How can i merge the rows as they basically are same thing

Name Date
first count of all rows containing first,First,FIRST

result would be

Name count
first 4

basically I want to count all rows with similar string using pandas

CodePudding user response:

print(len(df[df['Name'].str.lower() == 'first']))

Try this it will compare column values by making them to lowercase with the particular string and will let you know the final length.

CodePudding user response:

try:

df.groupby(df.Name.str.lower()).count()

Output:

       Name  Date
Name             
first     4     4

After that you can select the columns that you want like ['Date'].

In this case:

df.groupby(df.Name.str.lower()).count()['Date']

Output:

Name
first    4
Name: Date, dtype: int64

CodePudding user response:

in addition to the answer from @99_m4n , just posting the full code:

import pandas as pd


datadict = {'FIRST': 'some data', 'first': 'some data', 'First': 'some data', 'FirsT': 'some data'}

df = (
    pd.DataFrame({'Name': list(datadict.keys()), 'Date': list(datadict.values())})
    .assign(Name=lambda x: x["Name"].str.lower())
    .groupby(['Name']).agg({'Date':'count'}))
df
  • Related