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 the groupby method with the apply method.
For instance, we write
l = d.groupby('A')['B'].apply(list)
to call groupby with 'A' to group the rows in the d data frame by the values of column A.
Then we get the values of column B from the grouped items.
Next, we call apply with list to put the grouped items into their own list separated by group and return a new data frame with the values.
Conclusion
To get a union of strings with Python Pandas groupby, we can use the groupby method with the apply method.