from pandas.plotting import parallel_coordinates km=KMeans(n_clusters=2,random_state=10) km.fit(del_df) centers=km.labels_ customer=pd.DataFrame({'0':centers[0],"1":centers[1]}).T customer.columns=del_df.keys() df_median=pd.DataFrame({'2':del_df.median()}).T customer=pd.concat([customer,df_median]) customer["category"]=["customer_1","customer_2","median"] plt.figure(figsize=(12,6)) parallel_coordinates(customer,"category",color=('red','blue','black')) plt.xticks(rotation=15) plt.show() del_df['category']=labels del_df['category']=np.where(del_df.category == 0,'customer_1','customer_2') customer=pd.DataFrame({'0':centers[0],"1":centers[1]}).T customer['category'] = ['customer_1_center',"customer_2_center"] customer.columns=del_df.keys() del_df=pd.concat([del_df,customer]) #对6 类产品每年消费水平进行绘制图像 df_new = del_df[['Fresh','Milk','Grocery','Frozen','Detergents_Paper','Delicassen','category']] plt.figure(figsize=(18,6)) parallel_coordinates(df_new,"category",color=('red','blue','Magenta','RoyalBlue')) plt.xticks(rotation= 15) plt.show报错ValueError: If using all scalar values, you must pass an index()
时间: 2024-01-28 21:04:44 浏览: 24
根据你提供的代码和报错信息来看,可能是在创建 DataFrame 时出现了问题。错误提示中提到了 index() 方法,这个方法是用于获取 DataFrame 的行索引。你可以检查一下在创建 DataFrame 时是否有漏掉行索引的部分。
具体来说,可以检查以下代码中的 DataFrame 创建部分是否存在问题:
```
customer=pd.DataFrame({'0':centers[0],"1":centers[1]}).T
customer.columns=del_df.keys()
df_median=pd.DataFrame({'2':del_df.median()}).T
customer=pd.concat([customer,df_median])
customer["category"]=["customer_1","customer_2","median"]
```
你可以尝试添加一个行索引,例如:
```
customer=pd.DataFrame({'0':centers[0],"1":centers[1]}).T
customer.columns=del_df.keys()
df_median=pd.DataFrame({'2':del_df.median()}).T
customer=pd.concat([customer,df_median])
customer["category"]=["customer_1","customer_2","median"]
customer.index = ['row1', 'row2', 'row3'] # 尝试添加行索引
```
这样可能会解决报错问题。不过具体问题需要根据完整代码和数据集来进一步分析。