Concatenate strings from several rows using Pandas groupby
Pandas Dataframe.groupby() method is used to split the data into groups based on some criteria. The abstract definition of grouping is to provide a mapping of labels to the group name.
To concatenate string from several rows using Dataframe.groupby(), perform the following steps:
- Group the data using Dataframe.groupby() method whose attributes you need to concatenate.
- Concatenate the string by using the join function and transform the value of that column using lambda statement.
Example 1: We will concatenate the data in the branch column having the same name.
Python3
import pandas as pd
df = pd.read_csv( "Book2.csv" )
df[ 'branch' ] = df.groupby([ 'Name' ])[ 'branch' ].transform( lambda x : ' ' .join(x))
df = df.drop_duplicates()
print (df)
|
Output:
Example 2: We can perform Pandas groupby on multiple columns as well.
Apply groupby on Name and year column
Python3
import pandas as pd
df = pd.read_csv( "Book1.csv" )
df[ 'branch' ] = df.groupby([ 'Name' , 'year' ])[ 'branch' ].transform(
lambda x: ' ' .join(x))
df = df.drop_duplicates()
df
|
Output:
Last Updated :
20 Mar, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...