Categories
Python Answers

How to get a union of strings with Python Pandas groupby?

Spread the love

Sometimes, we want to get a union of strings with Python Pandas groupby.

In this article, we’ll look at how to get a union of strings with Python Pandas groupby.

How to get a union of strings with Python Pandas groupby?

To get a union of strings with Python Pandas groupby, we can use groupby with apply.

For instance, we write:

import pandas as pd

df = pd.DataFrame({'A': [1, 1, 3], 'B': [4, 5, 6]})
s = df.groupby('A')['B'].apply(list)
print(s)

to create the df data frame with pd.DataFrame.

Then we call df.groupby with 'A' and 'B' to group the values in column 'B' by the values of column 'A'.

And then we call apply with list to put the grouped values into lists.

Therefore, s is:

A
1    [4, 5]
3       [6]
Name: B, dtype: object

Conclusion

To get a union of strings with Python Pandas groupby, we can use groupby with apply.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *