Python 使用带熊猫的Pivot_表

Python 使用带熊猫的Pivot_表,python,pandas,Python,Pandas,我将Pandas与Python 3.7一起使用。我有一个包含8列的csv文件,我需要透视第5、6、7和8列。我是这样说的: pivot = pd.pivot_table (csv_file, values = ["column 5", "column 6" , "column 7","column 8"], index

我将Pandas与Python 3.7一起使用。我有一个包含8列的csv文件,我需要透视第5、6、7和8列。我是这样说的:

pivot = pd.pivot_table (csv_file, 
                        values = ["column 5", "column 6" , "column 7","column 8"],
                        index = "ID",
                        aggfunc = np.mean,
                        dropna = False)

然后我将其保存到csv。问题是保存的csv文件只有第5、6、7和8列。我希望它也保留前4列。我试图找到一些解决办法,比如从csv_文件中删除最后4列,然后在这些列旋转后再添加回来。我尝试过连接、连接和合并,但在旋转后用全部8列重新创建csv文件时没有成功。如果有人对如何执行此任务有任何见解或知识,我将非常感谢您的帮助

我没有您的csv_文件作为输入,但我尝试过这样做。看看这是不是你要找的

c1 = [i for i in range(10)]
c2 = [i for i in range(10)]
c3 = [str(i) for i in range (10)]
c4 = [str(i) for i in range (10,0,-1)]
c5 = ['test']*10

import pandas as pd
import numpy as np

df = pd.DataFrame({'column1':c1,'column2':c2,
                   'column3':c3,'column4':c4,'column5':c5,})

df_pivot=pd.pivot_table(df,
                  values=['column1', 'column2'], 
                  index=["column3","column4","column5"], 
                  aggfunc={'column1':[np.mean,max,min],'column2':np.sum})

#df_pivot.columns = df_pivot.columns.get_level_values(1)

df_pivot.columns = ['_'.join(col).strip() for col in df_pivot.columns.values]

print(df)
print(df_pivot)
column3,column4,column5,column1_max,column1_mean,column1_min,column2_sum
0,10,test,0,0,0,0
1,9,test,1,1,1,1
2,8,test,2,2,2,2
3,7,test,3,3,3,3
4,6,test,4,4,4,4
5,5,test,5,5,5,5
6,4,test,6,6,6,6
7,3,test,7,7,7,7
8,2,test,8,8,8,8
9,1,test,9,9,9,9
产出如下:

初始数据帧:

   column1  column2 column3 column4 column5
0        0        0       0      10    test
1        1        1       1       9    test
2        2        2       2       8    test
3        3        3       3       7    test
4        4        4       4       6    test
5        5        5       5       5    test
6        6        6       6       4    test
7        7        7       7       3    test
8        8        8       8       2    test
9        9        9       9       1    test
透视数据帧

                         column1_max  column1_mean  column1_min  column2_sum
column3 column4 column5                                                     
0       10      test               0             0            0            0
1       9       test               1             1            1            1
2       8       test               2             2            2            2
3       7       test               3             3            3            3
4       6       test               4             4            4            4
5       5       test               5             5            5            5
6       4       test               6             6            6            6
7       3       test               7             7            7            7
8       2       test               8             8            8            8
9       1       test               9             9            9            9
df_pivot.to_csv('out.csv', index_label=df_pivot.columns.name)
import openpyxl

with pd.ExcelWriter('myfile.xlsx',engine="openpyxl") as writer:
    df_pivot.to_excel(writer, sheet_name='Sheet1',columns=df_pivot.columns)
我有几分钟时间写你的问题陈述。您需要调出要写入文件的列。这就是我所做的

写入CSV文件

                         column1_max  column1_mean  column1_min  column2_sum
column3 column4 column5                                                     
0       10      test               0             0            0            0
1       9       test               1             1            1            1
2       8       test               2             2            2            2
3       7       test               3             3            3            3
4       6       test               4             4            4            4
5       5       test               5             5            5            5
6       4       test               6             6            6            6
7       3       test               7             7            7            7
8       2       test               8             8            8            8
9       1       test               9             9            9            9
df_pivot.to_csv('out.csv', index_label=df_pivot.columns.name)
import openpyxl

with pd.ExcelWriter('myfile.xlsx',engine="openpyxl") as writer:
    df_pivot.to_excel(writer, sheet_name='Sheet1',columns=df_pivot.columns)
其输出如下所示。我希望这就是你要找的

c1 = [i for i in range(10)]
c2 = [i for i in range(10)]
c3 = [str(i) for i in range (10)]
c4 = [str(i) for i in range (10,0,-1)]
c5 = ['test']*10

import pandas as pd
import numpy as np

df = pd.DataFrame({'column1':c1,'column2':c2,
                   'column3':c3,'column4':c4,'column5':c5,})

df_pivot=pd.pivot_table(df,
                  values=['column1', 'column2'], 
                  index=["column3","column4","column5"], 
                  aggfunc={'column1':[np.mean,max,min],'column2':np.sum})

#df_pivot.columns = df_pivot.columns.get_level_values(1)

df_pivot.columns = ['_'.join(col).strip() for col in df_pivot.columns.values]

print(df)
print(df_pivot)
column3,column4,column5,column1_max,column1_mean,column1_min,column2_sum
0,10,test,0,0,0,0
1,9,test,1,1,1,1
2,8,test,2,2,2,2
3,7,test,3,3,3,3
4,6,test,4,4,4,4
5,5,test,5,5,5,5
6,4,test,6,6,6,6
7,3,test,7,7,7,7
8,2,test,8,8,8,8
9,1,test,9,9,9,9
写入Excel文件

                         column1_max  column1_mean  column1_min  column2_sum
column3 column4 column5                                                     
0       10      test               0             0            0            0
1       9       test               1             1            1            1
2       8       test               2             2            2            2
3       7       test               3             3            3            3
4       6       test               4             4            4            4
5       5       test               5             5            5            5
6       4       test               6             6            6            6
7       3       test               7             7            7            7
8       2       test               8             8            8            8
9       1       test               9             9            9            9
df_pivot.to_csv('out.csv', index_label=df_pivot.columns.name)
import openpyxl

with pd.ExcelWriter('myfile.xlsx',engine="openpyxl") as writer:
    df_pivot.to_excel(writer, sheet_name='Sheet1',columns=df_pivot.columns)
我得到的答案如下:


看来这有助于解决问题。如果这样做了,请阅读当有人问你问题时该怎么办。不幸的是,这对我不起作用。我更改了代码以模仿您的建议。我不知道您为什么要加入下划线,但无论有无该行,保存的csv文件仍然只包含最后4列。让我编写代码,将数据推送到csv文件中,并将其提供给您。看看这是否对您有帮助。这导致了我正在处理的同一个问题。看起来唯一的区别是索引是在值之前传入的。