Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combine rows into single cell

I currently have a dataframe (df) like this:

name    info
alpha   foo,bar
alpha   bar,foo
beta    foo,bar
beta    bar,foo
beta    baz,qux

I'm looking to create a dataframe like this:

name    info
alpha   (foo,bar),(bar,foo)
beta    (foo,bar),(bar,foo),(baz,qux)

I'm getting close with groupby.apply(list). Eg.

new_df=df.groupby('name')['info'].apply(list)

However, I can't seem to figure out how to get the output in the original dataframe format. (i.e with two columns (like the example)

I think I need reset_index and unstack? Appreciate any help!

like image 306
Kvothe Avatar asked Jul 07 '26 16:07

Kvothe


2 Answers

Try following using for loop:

uniqnames = df.name.unique() # get unique names
newdata = []                 # data list for output dataframe
for u in uniqnames:          # for each unique name
    subdf = df[df.name == u] # get rows with this unique name
    s = ""
    for i in subdf['info']:
        s += "("+i+"),"      # join all info cells for that name
    newdata.append([u, s[:-1]]) # remove trailing comma from infos & add row to data list

newdf = pd.DataFrame(data=newdata, columns=['name','info'])
print(newdf)

Output is exactly as desired:

    name                           info
0  alpha            (foo,bar),(bar,foo)
1   beta  (foo,bar),(bar,foo),(baz,qux)
like image 172
rnso Avatar answered Jul 10 '26 06:07

rnso


IIUC

df.assign(info='('+df['info']+')').groupby('name')['info'].apply(','.join).to_frame('info')
Out[267]: 
                                info
name                                
alpha            (foo,bar),(bar,foo)
beta   (foo,bar),(bar,foo),(baz,qux)



#df.assign(info='('+df['info']+')')# adding the ( and ) for your single string to match with the out put 
#groupby('name')# group by the name, you need merge info under the same name
#apply(','.join).to_frame('info') # this will combine each info into one string under the same group
like image 24
BENY Avatar answered Jul 10 '26 06:07

BENY