简体   繁体   English

转置 Pandas 数据透视表

[英]Transpose Pandas Pivot Table

I created a pandas pivot table using pd.pivot_table .我使用pd.pivot_table创建了一个pd.pivot_table数据透视表。 My table has one dependent variable, and then three independent variables (1 for rows, 2 for columns).我的表有一个因变量,然后是三个自变量(1 个用于行,2 个用于列)。 I want to export the table so all the values are in one row.我想导出表,以便所有值都在一行中。 However, when I try to unstack() the table, instead of moving the row variable to the columns, it moves all the columns variables to the rows - leaving all the values in one column instead.但是,当我尝试unstack()表时,它不是将行变量移动到列,而是将所有列变量移动到行 - 将所有值留在一列中。

Is there a way to transpose this so when I export it to excel all the values are in one row?有没有办法转置这个,所以当我将它导出到excel时,所有值都在一行中?

You are producing a Series by stacking (or unstacking), which is actually one-dimensional and displays as a column.您正在通过堆叠(或取消堆叠)生成一个系列,它实际上是一维的并显示为一列。 You could force to display as a row with something like this:您可以强制显示为一行,如下所示:

pd.DataFrame(df.stack()).T

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM