如何在 pandas 中融化数据框并连接值的字符串

How do I melt a dataframe in pandas AND concatenate strings for the value

假设我有一个数据框

我想重塑它并连接字符串

我可以使用 melt 重塑它,但我丢失了描述。我试过变换但没有运气 有什么想法吗?

代码:

import pandas as pd
x = [['a', 'Electronics', 'TV', '42" plasma'], ['a', 'Electronics', 'TV', '36" LCD'], ['a', 'Electronics', 'hifi', 'cd player'], ['a', 'Electronics', 'hifi', 'record player'], ['b', 'Sports', 'Soccer', 'mens trainers'], ['b', 'Sports', 'Soccer', 'womens trainers'], ['b', 'Sports', 'golf', '9 iron']]

df = pd.DataFrame(x, columns =['id', 'category','sub_category','description'])
y = pd.melt(df, id_vars=['id'],value_vars=['category','sub category'])['description'].transform(lambda x : ' '.join(x))

第一个问题melt,需要将description列添加到id_vars,然后聚合joingroupby,所以加起来是:

y = (pd.melt(df, 
             id_vars=['id','description'],
             value_vars=['category','sub_category'], 
             value_name='Category')
       .groupby(['id','Category'])['description']
       .agg(' '.join)
       .reset_index())


print (y)
  id     Category                                 description
0  a  Electronics  42" plasma 36" LCD cd player record player
1  a           TV                          42" plasma 36" LCD
2  a         hifi                     cd player record player
3  b       Soccer               mens trainers womens trainers
4  b       Sports        mens trainers womens trainers 9 iron
5  b         golf                                      9 iron