我试图以一种格式压缩我的数据,这样它会更有用,数据格式:
Table1
Key AttName Rank
1 Color 1
1 type 2
1 kkk 3
2 Color 1
2 type 2
如上图所示,例如,一些键有 2 个属性,而其他键有 3 个属性,所以当我创建数据透视表然后尝试标记它不起作用的列名时,hpw 我可以更改它吗,正如上面显示的那样,键 1 有 3 个属性名称而 Key 2 只有 2 个导致错误发生
最终数据:
Table2
Family Assortment Group Key Attribute Name Attribute Value
a ab 1 Color Green
a1 ab 1 Color Yellow
a2 ab 1 type shirt
a6 ab 1 kkkk f
a3 ab 2 Color Red
a4 ab 2 Type TShirt
a5 ab 2 Color Yellow
代码
#For loop that loops over key values; key values defined as Zone AG
Finals=[]
Finals2=[]
Finals=pd.DataFrame(Finals)
Finals2=pd.DataFrame(Finals)
for group in Select.groupby('Key'):
# group is a tuple where the first value is the Key and the second is the dataframe
Final2=group[1]
Family1=Family.merge(Final2, on='Key1', how='inner')
result=Family1.pivot_table(index=['Family','Assortment Group','Key'], columns='Attribute Name', values='Attribute Value', aggfunc='first').reset_index()
result.columns=['Family','Assortment Group','Key','Att1','Att2','Att3']
Finals=Finals.append(result)
追溯
ValueError: Length mismatch: Expected axis has 5 elements, new values have 6 elements
慕妹3242003
相关分类