Skip to main content
 首页 » 编程设计

python之合并包含列表的数据框列

2024年10月24日10开发

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

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 有什么想法吗?

请您参考如下方法:

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