我通過提供 2 列作為數(shù)據(jù)透視輸入,從數(shù)據(jù)框中創(chuàng)建數(shù)據(jù)透視表,如下面的代碼所示。應用樞軸后,我需要使用此堆疊輸出與類似的列數(shù)據(jù)框合并,因為我想丟棄也出現(xiàn)在索引名稱上方的名稱(“品牌和類型”),我該如何實現(xiàn)這一點。請注意我想保留堆疊輸出import pandas as pdcars = {'day':['aug','aug','sep','sep','aug'], 'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4','Hyundai Elite i20'], 'Type':['sedan,','sedan','hatchback','hatchback','hatchback'], 'Down Price': [22000,25000,27000,35000,10000] }df = pd.DataFrame(cars, columns = ['day','Brand', 'Type','Down Price'])dfpivot=pd.pivot_table(df,index=['day'],columns=['Brand','Type'],values=['Down Price'],aggfunc=np.max)dfpivot.columns = dfpivot.columns.droplevel(0)我嘗試重置索引但沒有成功dfpivot.rename_axis(None, axis=1).reset_index()然后我嘗試刪除索引,但這也不起作用。如何刪除出現(xiàn)在索引上方的名稱、品牌和類型。先感謝您。添加溶液后dfpivot.columns.name=[None,None]追溯:---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-102-ef1eb88ee6b2> in <module> 10 dfpivot=pd.pivot_table(df,index=['day'],columns=['Brand','Type'],values=['Down Price'],aggfunc=np.max) 11 ---> 12 dfpivot.columns.name=[None,None]~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in name(self, value) 1192 "'MultiIndex.set_names' instead." 1193 )-> 1194 maybe_extract_name(value, None, type(self)) 1195 self._name = value 1196 ~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in maybe_extract_name(name, obj, cls) 5402 # GH#29069 5403 if not is_hashable(name):-> 5404 raise TypeError(f"{cls.__name__}.name must be a hashable type") 5405 5406 return nameTypeError: MultiIndex.name must be a hashable type
Pandas 堆棧輸出格式化后數(shù)據(jù)透視表
慕尼黑5688855
2023-08-08 17:34:12