Merge rows in pandas dataframe and sum them

Question:

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

Asked By: Sanket Wagh

||

Answers:

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.

Answered By: Unkown

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
Answered By: 99_m4n

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
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.