I am creating a Dataframe
import pandas as pd
df1 = pd.DataFrame( {
"Name" : ["Alice", "Bob", "Mallory", "Mallory", "Bob" , "Mallory"] ,
"City" : ["Seattle", "Seattle", "Portland", "Seattle", "Seattle",
"Portland"] } )
df1.groupby( ["City"] )['Name'].transform(lambda x:
','.join(x)).drop_duplicates()
I want the output as
Name City
Alice,Bob,Mallory,Bob Seattle
Mallory,Mallory Portland
but i am getting only
Name
Alice,Bob,Mallory,Bob
Mallory,Mallory
This is an example with small number of columns but in my actual problem i
have too many columns so i cannot use
df1['Name']= df1.groupby( ['City'] )['Name'].transform(lambda x:
','.join(x))
df1.groupby( ['City','Name'], as_index=False )
df1.drop_duplicates()
because for each column i have to write the same code
Is there any way to do it without writing transform for each column
individually.