过滤Pandas数据透视表

时间:2017-10-19 14:49:36

标签: python excel pandas pivot-table

我尝试通过SQL查询为Pandas提供一些数据,然后将转化的结果保存到.csv文件中。到目前为止,我已经取得了成功。

import cx_Oracle
import pandas as pd

query = """
        SELECT 
            dt,
            --Workstack, 
            GM, 
            COUNT(JOB_NUMBER)
        FROM MY_TABLE
        GROUP BY 
            dt, 
            --Workstack, 
            GM
"""

connection = cx_Oracle.connect("<My_Username>", "<My Password>", "<Database String>")
df = pd.read_sql_query(query, connection)
piv = df.pivot(index='GM', columns='DT', values='COUNT')
print (piv.head())

piv.to_csv('''Workstack.csv''')

现在我希望将字段包含在#34; Workstack&#34;在查询变量中使用,我也想使用pandas在字段Workstack上过滤不同的字符串,然后将它们粘贴为不同的csv文件或同一个Excel文件。但是,当我在查询中包含Workstack时,python给了我这个错误......

ValueError: Index contains duplicate entries, cannot reshape

我是如何解决这个问题的?最后我想使用python Excel模块将像上面那样的数据透视表粘贴到不同的单元格和工作表上。

谢谢:)

2 个答案:

答案 0 :(得分:1)

尝试pivot_table聚合功能:

piv = df.pivot_table(index='GM', columns='DT', values='COUNT', aggfunc='sum')

答案 1 :(得分:0)

import cx_Oracle
import pandas as pd

query = """SELECT * FROM MY_TABLE"""

print('Loading Query')

connection = cx_Oracle.connect("Username", "Password", "Data Connection")
df = pd.read_sql_query(query, connection)

PivotTable = df.loc[df['ColumnName'] == "ValueinColumn"].pivot_table(index='SelectRows', columns='SelectColumns', values='SelectValues', aggfunc='SelectAggregation')

print ("Writing to Excel")

ExcelWorkbook = pd.ExcelWriter('Data.xlsx')
PivotTable.to_excel(ExcelWorkbook,  sheet_name='PivotTable', startrow=1)
ExcelWorkbook.save()

print ("Closing Connection")
connection.close()

print('Section Complete')