Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何通过ParentAuction列透视表,从而获得如图所示的输出_Python_Pandas_Pivot Table - Fatal编程技术网

Python 如何通过ParentAuction列透视表,从而获得如图所示的输出

Python 如何通过ParentAuction列透视表,从而获得如图所示的输出,python,pandas,pivot-table,Python,Pandas,Pivot Table,我如何使用pandas pivot表以这样的方式获取输出,即所有Make都被各自的ParentAuction值分割 像这样,, 当我运行这个时 pd.pivot_table(df,columns=['Make','Sales','AVG PMV','AVG GrossProfit','Loss%'],values=['ParentAuction']) 我得到这个错误 pandas.core.base.DataError:没有要聚合的数值类型 您需要添加'aggfunc'参数。大概是这样的:

我如何使用pandas pivot表以这样的方式获取输出,即所有Make都被各自的ParentAuction值分割

像这样,,

当我运行这个时

pd.pivot_table(df,columns=['Make','Sales','AVG PMV','AVG GrossProfit','Loss%'],values=['ParentAuction'])
我得到这个错误
pandas.core.base.DataError:没有要聚合的数值类型

您需要添加'aggfunc'参数。大概是这样的:

pd.pivot_table(df,columns=['Make','Sales','AVG PMV','AVG GrossProfit','Loss%'],values=['ParentAuction'], aggfunc = 'count')
让我们试试这个:

df.set_index(['Make','ParentAuction']).unstack().swaplevel(0,1,axis=1).sort_index(axis=1)
输出:

ParentAuction          Copart                                   IAA          \
              AVG GrossProfit AVG PMV  Loss%  Sales AVG GrossProfit AVG PMV   
Make                                                                          
Acura                  112.99  -15.53  36.46   96.0             NaN     NaN   
Audi                   150.85  -13.04  32.95   88.0             NaN     NaN   
BMW                    134.39  -14.65  34.91  212.0          185.62  -11.92   
Buick                    6.35  -29.42  46.97   66.0           90.90  -26.47   
Cadillac                91.71  -17.88  41.46   82.0             NaN     NaN   
Chevrolet              133.87  -14.06  35.82  776.0          150.29  -12.04   
Chrysler                83.15   17.14  38.66  194.0             NaN     NaN   
Dodge                   99.07  -18.68  37.60  383.0          154.23  -12.10   
Ford                   122.57  -15.88  37.79  979.0          169.51  -12.58   
GMC                    107.94  -16.63  41.45  152.0          113.92  -13.19   

ParentAuction                 
               Loss%   Sales  
Make                          
Acura            NaN     NaN  
Audi             NaN     NaN  
BMW            27.14   210.0  
Buick          47.22    72.0  
Cadillac         NaN     NaN  
Chevrolet      29.82   912.0  
Chrysler         NaN     NaN  
Dodge          31.46   426.0  
Ford           30.69  1284.0  
GMC            33.08   133.0  

使用“ParentAuction”代替“ParentAuction”的值作为列参数,即与pivot表非常相似,但使用pivot表

df.pivot_table(index='Make',columns=['ParentAuction']).swaplevel(0,1,axis=1).sort_index(axis=1)
第三部分IAA\ 平均总利润平均PMV损失百分比销售平均总利润平均PMV 制作 Acura 112.99-15.53 36.46 96.0楠楠楠 奥迪150.85-13.04 32.95 88.0楠楠楠 宝马134.39-14.65 34.91 212.0 185.62-11.92 别克6.35-29.42 46.97 66.0 90.90-26.47 凯迪拉克91.71-17.88 41.46 82.0楠楠楠 雪佛兰133.87-14.06 35.82 776.0 150.29-12.04 克莱斯勒83.15 17.14 38.66 194.0楠楠楠 道奇99.07-18.68 37.60 383.0 154.23-12.10 福特122.57-15.88 37.79 979.0 169.51-12.58 GMC 107.94-16.63 41.45 152.0 113.92-13.19 家长拍卖 销售损失% 制作 讴歌楠楠 奥迪楠楠 宝马27.14 210.0 别克47.22 72.0 凯迪拉克楠楠 雪佛兰29.82 912.0 克莱斯勒南南 道奇31.46426.0 福特30.69 1284.0 GMC 33.08 133.0
我不知道
swaplevel
sort\u index
。它也适用于透视表。这太棒了。 ParentAuction Copart IAA \ AVG GrossProfit AVG PMV Loss% Sales AVG GrossProfit AVG PMV Make Acura 112.99 -15.53 36.46 96.0 NaN NaN Audi 150.85 -13.04 32.95 88.0 NaN NaN BMW 134.39 -14.65 34.91 212.0 185.62 -11.92 Buick 6.35 -29.42 46.97 66.0 90.90 -26.47 Cadillac 91.71 -17.88 41.46 82.0 NaN NaN Chevrolet 133.87 -14.06 35.82 776.0 150.29 -12.04 Chrysler 83.15 17.14 38.66 194.0 NaN NaN Dodge 99.07 -18.68 37.60 383.0 154.23 -12.10 Ford 122.57 -15.88 37.79 979.0 169.51 -12.58 GMC 107.94 -16.63 41.45 152.0 113.92 -13.19 ParentAuction Loss% Sales Make Acura NaN NaN Audi NaN NaN BMW 27.14 210.0 Buick 47.22 72.0 Cadillac NaN NaN Chevrolet 29.82 912.0 Chrysler NaN NaN Dodge 31.46 426.0 Ford 30.69 1284.0 GMC 33.08 133.0