Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.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 使用数据集列作为行的透视表的有效方法是什么?_Python_Pandas_Pivot Table - Fatal编程技术网

Python 使用数据集列作为行的透视表的有效方法是什么?

Python 使用数据集列作为行的透视表的有效方法是什么?,python,pandas,pivot-table,Python,Pandas,Pivot Table,让我们以以下数据集为例: make address all 3d our over length_total y 0 0.0 0.64 0.64 0.0 0.32 0.0 278 1 1 0.21 0.28 0.5 0.0 0.14 0.28 1028 1 2 0.06 0.0 0.71 0.0 1.23

让我们以以下数据集为例:

    make    address all     3d   our    over    length_total    y
0   0.0     0.64    0.64    0.0  0.32   0.0     278             1
1   0.21    0.28    0.5     0.0  0.14   0.28    1028            1
2   0.06    0.0     0.71    0.0  1.23   0.19    2259            1
3   0.15    0.0     0.46    0.1  0.61   0.0     1257            1
4   0.06    0.12    0.77    0.0  0.19   0.32    749             1
5   0.0     0.0     0.0     0.0  0.0    0.0     21              1
6   0.0     0.0     0.25    0.0  0.38   0.25    184             1
7   0.0     0.69    0.34    0.0  0.34   0.0     261             1
8   0.0     0.0     0.0     0.0  0.9    0.0     25              1
9   0.0     0.0     1.42    0.0  0.71   0.35    205             1
10  0.0     0.0     0.0     0.0  0.0    0.0     23              0
11  0.48    0.0     0.0     0.0  0.48   0.0     37              0
12  0.12    0.0     0.25    0.0  0.0    0.0     491             0
13  0.08    0.08    0.25    0.2  0.0    0.25    807             0
14  0.0     0.0     0.0     0.0  0.0    0.0     38              0
15  0.24    0.0     0.12    0.0  0.0    0.12    227             0   
16  0.0     0.0     0.0     0.0  0.75   0.0     77              0
17  0.1     0.0     0.21    0.0  0.0    0.0     571             0
18  0.51    0.0     0.0     0.0  0.0    0.0     74              0
19  0.3     0.0     0.15    0.0  0.0    0.15    155             0
我想从前面的数据集中获取pivot表,其中的列(make、address all、3d、our、over、length_total)的平均值将由y列处理。下表为预期结果:

                    y   
                    1   0
make            0.048   0.183
address         0.173   0.008
all             0.509   0.098
3d              0.01    0.02
our             0.482   0.123
over            0.139   0.052
length_total    626.7   250
是否可以通过pivot_table方法从pandas.data对象获得所需的结果?如果是,怎么做


有更有效的方法吗?

有些人喜欢使用
堆栈或
取消堆栈
,但我更喜欢使用好的ol'来“展平”或“取消”帧:

(如果要将原始列顺序保留为新行顺序,可以使用
.loc
对此进行索引,例如
df2.loc[df.columns].dropna()


Melling执行展平,并将
y
保留为列,将旧列名作为名为
“variable”
的新列(如果愿意,可以更改):


之后,我们可以像平常一样调用
pivot\u table

下面是使用Pandas v 0.13.1的一个小改动。df_m.pivot_table(rows=“variable”,cols=“y”)感谢您的回答!这真的很有帮助!
>>> df_m = pd.melt(df, id_vars="y")
>>> df_m.pivot_table(index="variable", columns="y")
                value         
y                   0        1
variable                      
3d              0.020    0.010
address         0.008    0.173
all             0.098    0.509
length_total  250.000  626.700
make            0.183    0.048
our             0.123    0.482
over            0.052    0.139
>>> pd.melt(df, id_vars="y").head()
   y variable  value
0  1     make   0.00
1  1     make   0.21
2  1     make   0.06
3  1     make   0.15
4  1     make   0.06