Home > Blockchain >  Group by Pandas and combine multipe strings
Group by Pandas and combine multipe strings

Time:08-14

I have a dataframe currently which resembles this

Source | Destination | Type
A | B | Insert
A | B | Delete
B | C | Insert

What I want to achieve is something like this

Source | Destination | Type
A | B | Insert, Delete
B | C | Insert

I tried using group by Source and Destination but Im a little unsure how do I append to the type. Any ideas?

CodePudding user response:

Check Below Code:

df.groupby(['Source','Destination']).agg({'Type':'unique'})

CodePudding user response:

Figured out something

df = df.groupby(['Source','Destination'])['Type'].apply(lambda x: ','.join(x)).reset_index()

CodePudding user response:

Make a string separated by ", ":

df.groupby(['Source', 'Destination']).agg(', '.join).reset_index()

Make a list:

df.groupby(['Source','Destination']).agg(list).reset_index()

CodePudding user response:

Joining by , and building a string works but it is not very convenient if you later want to perform other operations with this column value. (such as iterating over the elements.)

This creates a set of values.

column_map: Dict[str,Any] = {} column_map["Type"] = lambda x: set(x)

df.groupby(["Source", "Destination"]).agg(column_map)

Source | Destination | Type
A | B | {Insert Delete}
B | C | {Insert}

if you instead want to get a list and dont want eliminate duplicates. Just replace set(x) with list(x)

  • Related