Pandas: How to Concatenate Strings from Using GroupBy


You can use the following basic syntax to concatenate strings from using GroupBy in pandas:

df.groupby(['group_var'], as_index=False).agg({'string_var': ' '.join})

This particular formula groups rows by the group_var column and then concatenates the strings in the string_var column.

The following example shows how to use this syntax in practice.

Example: How to Concatenate Strings from Using GroupBy

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'store': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'quarter': [1, 1, 2, 2, 1, 1, 2, 2],
                   'employee': ['Andy', 'Bob', 'Chad', 'Diane',
                                'Elana', 'Frank', 'George', 'Hank']})

#view DataFrame
print(df)

We can use the following syntax to group the rows of the DataFrame by store and quarter and then concatenate the strings in the employee column:

#group by store and quarter, then concatenate employee strings
df.groupby(['store', 'quarter'], as_index=False).agg({'employee': ' '.join})

	store	quarter	employee
0	A	1	Andy Bob
1	A	2	Chad Diane
2	B	1	Elana Frank
3	B	2	George Hank

The result is a DataFrame grouped by store and quarter with the strings in the employee column concatenated together with a space.

We could also concatenate the strings using a different separator such as the & symbol:

#group by store and quarter, then concatenate employee strings
df.groupby(['store', 'quarter'], as_index=False).agg({'employee': ' & '.join})

	store	quarter	 employee
0	A	1	 Andy & Bob
1	A	2	 Chad & Diane
2	B	1	 Elana & Frank
3	B	2	 George & Hank

Notice that the strings in the employee column are now separated by the & symbol.

Note: You can find the complete documentation for the GroupBy operation in pandas here.

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

Pandas: How to Calculate Cumulative Sum by Group
Pandas: How to Count Unique Values by Group
Pandas: How to Calculate Correlation By Group

Leave a Reply

Your email address will not be published.