我试图将我的数据压缩成一种更有用的格式,数据格式:
Table1
Key AttName Rank
1 Color 1
1 type 2
1 kkk 3
2 Color 1
2 type 2
如上图所示,一些键有2个att,而另一些键有3个att,因此当我创建透视表并尝试标记列名时,hpw是否可以更改此设置,如上图所示,键1有3个属性名,而键2只有2个att,因此发生错误
最终数据:
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
您可以使用列表理解重命名列,以生成适当数量的Att列
编辑:解释,
result.shape[1]
给出结果中的列数。假设它是5,那么前3个是“家庭”、“分类组”、“钥匙”。因此,您需要再创建两个Att,在本例中range(1, result.shape[1]-2)
是range(1, 3)
,然后[f'Att{i}' for i in range(1, result.shape[1]-2)]
将迭代i=1和i=2以创建列表['Att1', 'Att2']
。将此列表添加到具有3个第一列名称的列表中,以获得正确的列数相关问题 更多 >
编程相关推荐