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
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
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.