Reputation: 363
I have a dataframe that contains a list :
id rl
a [c,v,b,n]
b []
c [x,a]
....
and i want to merge the cells like this
merged
a,c,v,b,n
b
c,x,a
I tried this pd.DataFrame({'merged' : fdf.groupby(['id'])['rel'].apply(",".join)}).reset_index()
but i get this error expected str instance, list found
Any ideas?
Upvotes: 2
Views: 900
Reputation: 5414
In [11]:
ind = pd.Series(df.index)
ind
Out[11]:
0 a
1 b
2 c
dtype: object
In [20]:
list_string = df.rl.map(lambda x : ','.join(x))
list_string
Out[20]:
a c,v,b,n
b
c x,a
Name: rl, dtype: object
In [22]:
final = ind.str.cat(list_string, sep = ',')
final
Out[22]:
0 a,c,v,b,n
1 b,
2 c,x,a
dtype: object
# to remove the comma at the end of string you can simply do the following
# final.str.replace(',$' , '')
# 0 a,c,v,b,n
# 1 b
# 2 c,x,a
# dtype: object
In [24]:
pd.DataFrame(final , columns=['merged'])
Out[24]:
merged
0 a,c,v,b,n
1 b,
2 c,x,a
Upvotes: 2