【发布时间】:2022-01-11 23:40:16
【问题描述】:
我有以下数据结构。其中类别名称与产品名称混合在一起
df = pd.DataFrame(data={'name':['Category A', 'Subcategory A.A', 'Product A', 'Product B', 'Category B', 'Product C'],'values':["", "", 1,2,"", 3]})
name values
Category A
Subcategory A.A
Product A 1
Product B 2
Category B
Product C 3
name 列中没有值的每个条目都是类别名称。
有没有办法将pandas DataFrame 转换成如下结构?
name values category
Product A 1 Category A, Subcategory A.A
Product B 2 Category A, Subcategory A.A
Product C 3 Category B
感谢任何帮助。
【问题讨论】:
标签: python pandas dataframe group-by pandas-groupby