how to concat sets when using groupby in pandas dataframe?

ALH

This is my dataframe:

> df
       a             b
    0  1         set([2, 3])
    1  2         set([2, 3])
    2  3      set([4, 5, 6])
    3  1  set([1, 34, 3, 2])

Now when I groupby, I want to update sets. If it was a list there was no problem. But the output of my command is:

> df.groupby('a').sum()

a         b                
1             NaN
2     set([2, 3])
3  set([4, 5, 6])  

What should I do in groupby to update sets? The output I'm looking for is as below:

a         b                
1     set([2, 3, 1, 34])
2     set([2, 3])
3     set([4, 5, 6])  
matt_s

This might be close to what you want

df.groupby('a').apply(lambda x: set.union(*x.b))

In this case it takes the union of the sets.

If you need to keep the column names you could use:

df.groupby('a').agg({'b':lambda x: set.union(*x)}).reset_index('a')

Result:

    a   b
0   1   set([1, 2, 3, 34])
1   2   set([2, 3])
2   3   set([4, 5, 6])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Pandas transform dataframe using groupby when count of a string in a column is maximum

From Dev

StopIteration error when using groupby method of Pandas DataFrame

From Dev

How to concat Pandas dataframe columns

From Dev

How to get other columns when using Spark DataFrame groupby?

From Dev

Conditionally concat a dataframe in python using pandas

From Dev

How to sum negative and positive values separately when using groupby in pandas?

From Dev

Using a pandas dataframe how to aggregate and groupby and bring in non aggregated/groupby columns

From Dev

using groupby on pandas dataframe to group by financial year

From Dev

Calculate STD manually using Groupby Pandas DataFrame

From Dev

Pandas: DataFrame filtering using groupby and a function

From Dev

using groupby on pandas dataframe to group by financial year

From Dev

Calculate STD manually using Groupby Pandas DataFrame

From Dev

pandas groupby of DataFrame using Series of substrings

From Dev

how to drop duplicates in pandas when entries are sets

From Java

How to update a pandas dataframe with sets, from another dataframe

From Dev

splitting pandas dataframe into training and test sets when indexed by time

From Dev

splitting pandas dataframe into training and test sets when indexed by time

From Dev

how to groupby pandas dataframe on some condition

From Java

How to group dataframe rows into list in pandas groupby

From Java

How to GroupBy a Dataframe in Pandas and keep Columns

From Dev

How to efficiently columnize (=pivoting) pandas DataFrame (with groupby)?

From Dev

How to groupby consecutive values in pandas DataFrame

From Dev

How to groupby pandas DataFrame by customized function

From Dev

How to groupby with certain condition in pandas dataframe

From Dev

concat a DataFrame with a Series in Pandas

From Dev

How do I get the first timestamp (index) of a group when applying groupby to a python pandas dataframe?

From Dev

How can I concatenate date from another column when I use groupby and aggregation in a pandas dataframe

From Dev

Include empty series when creating a pandas dataframe with .concat

From Dev

Find the majority in dataframe using pandas.DataFrame.mode and groupby

Related Related

  1. 1

    Pandas transform dataframe using groupby when count of a string in a column is maximum

  2. 2

    StopIteration error when using groupby method of Pandas DataFrame

  3. 3

    How to concat Pandas dataframe columns

  4. 4

    How to get other columns when using Spark DataFrame groupby?

  5. 5

    Conditionally concat a dataframe in python using pandas

  6. 6

    How to sum negative and positive values separately when using groupby in pandas?

  7. 7

    Using a pandas dataframe how to aggregate and groupby and bring in non aggregated/groupby columns

  8. 8

    using groupby on pandas dataframe to group by financial year

  9. 9

    Calculate STD manually using Groupby Pandas DataFrame

  10. 10

    Pandas: DataFrame filtering using groupby and a function

  11. 11

    using groupby on pandas dataframe to group by financial year

  12. 12

    Calculate STD manually using Groupby Pandas DataFrame

  13. 13

    pandas groupby of DataFrame using Series of substrings

  14. 14

    how to drop duplicates in pandas when entries are sets

  15. 15

    How to update a pandas dataframe with sets, from another dataframe

  16. 16

    splitting pandas dataframe into training and test sets when indexed by time

  17. 17

    splitting pandas dataframe into training and test sets when indexed by time

  18. 18

    how to groupby pandas dataframe on some condition

  19. 19

    How to group dataframe rows into list in pandas groupby

  20. 20

    How to GroupBy a Dataframe in Pandas and keep Columns

  21. 21

    How to efficiently columnize (=pivoting) pandas DataFrame (with groupby)?

  22. 22

    How to groupby consecutive values in pandas DataFrame

  23. 23

    How to groupby pandas DataFrame by customized function

  24. 24

    How to groupby with certain condition in pandas dataframe

  25. 25

    concat a DataFrame with a Series in Pandas

  26. 26

    How do I get the first timestamp (index) of a group when applying groupby to a python pandas dataframe?

  27. 27

    How can I concatenate date from another column when I use groupby and aggregation in a pandas dataframe

  28. 28

    Include empty series when creating a pandas dataframe with .concat

  29. 29

    Find the majority in dataframe using pandas.DataFrame.mode and groupby

HotTag

Archive