python-3.x 如何合并Pandas中的 Dataframe 正常列和集合列?

jv2fixgn  于 2023-02-01  发布在  Python
关注(0)|答案(3)|浏览(176)

我有一个 Dataframe df

item        Space   rem_spc     nxt_item
Pineapple   0.5     0.5         {Mango, Grape}

我需要将df['item']df['nxt_item']合并为一列df['com_item'],如下所示

item        Space   rem_spc     nxt_item        com_item
Pineapple   0.5     0.5         {Mango, Grape}  Pineapple,Mango,Grape

谢谢!

svdrlsy4

svdrlsy41#

使用pandas.Series.strip时:

df["com_item"] = df["item"] + "," + df["nxt_item"].astype(str).str.strip("{}")

输出:

print(df)

        item  Space  rem_spc        nxt_item                 com_item
0  Pineapple    0.5      0.5  {Mango, Grape}  Pineapple, Mango, Grape
vyswwuz2

vyswwuz22#

如果希望nxt_item作为列表:

df['com_item'] = df.apply(lambda row: list(row['nxt_item']) + [row['item']] ,axis=1)

输出:

item  Space  rem_spc        nxt_item                   com_item
0  pineapple    0.5      0.5  {Grape, Mango}  [Grape, Mango, pineapple]

如果要作为字符串:

df['com_item'] = df.apply(lambda row: ' '.join(list(row['nxt_item']) + [row['item']] ),axis=1)

输出:

item  Space  rem_spc        nxt_item               com_item
0  pineapple    0.5      0.5  {Grape, Mango}  Grape Mango pineapple
z9smfwbn

z9smfwbn3#

对集合列使用Series.str.join并添加item值:

df['com_item'] = df['item'] + ',' + df['nxt_item'].str.join(',')
print (df)
        item  Space  rem_spc        nxt_item               com_item
0  Pineapple    0.5      0.5  {Grape, Mango}  Pineapple,Grape,Mango

或者使用Series.str.cat

df['com_item'] = df['item'].str.cat( df['nxt_item'].str.join(','), sep=',')

如果需要,可以在com_item中为已消除重复数据的值创建第一个集合,将值item添加到集合中,并在列表解析中加入:

df['com_item'] = [','.join(b.union({a})) for a, b in zip(df['item'],df['nxt_item'])]

样品数据见溶液差异:

print (df)
        item  Space  rem_spc           nxt_item
0  Pineapple    0.5      0.5  {'Mango','Grape'}
1      Mango    0.5      0.5  {'Mango','Grape'}

df['nxt_item'] = df['nxt_item'].apply(ast.literal_eval)

df['com_item1'] = [','.join(b.union({a})) for a, b in zip(df['item'],df['nxt_item'])]
df['com_item2'] = df['item'] + ',' + df['nxt_item'].str.join(',')

print (df)
        item  Space  rem_spc        nxt_item              com_item1  \
0  Pineapple    0.5      0.5  {Grape, Mango}  Grape,Mango,Pineapple   
1      Mango    0.5      0.5  {Grape, Mango}            Grape,Mango   

               com_item2  
0  Pineapple,Grape,Mango  
1      Mango,Grape,Mango

相关问题