合并包含列表的数据框列

时间:2015-11-16 13:09:57

标签: python pandas dataframe

我有一个包含列表的数据框:

id  rl
a   [c,v,b,n]
b   []
c   [x,a]
....

我希望合并像这样的单元格

merged
a,c,v,b,n
b
c,x,a

我试过这个pd.DataFrame({'merged' : fdf.groupby(['id'])['rel'].apply(",".join)}).reset_index(),但我收到此错误expected str instance, list found 有什么想法吗?

1 个答案:

答案 0 :(得分:1)

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