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

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.