简体   繁体   中英

Filtering Pandas PivotTable

I'm trying to feed Pandas some data via a SQL query then save the pivoted results onto a .csv file. So far I have been successful.

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''')

Now I wish to include the field "Workstack" used within the query variable and I also want to use pandas to filter on different strings with the field Workstack then paste them as different csv files or on the same Excel file. However, when I include Workstack within the query python gives me this error...

ValueError: Index contains duplicate entries, cannot reshape

Any ideas how I solve this? Eventually I want to use a python Excel module to paste pivot tables like the one above onto different cells and sheets.

Thanks :)

使用聚合函数尝试pivot_table

piv = df.pivot_table(index='GM', columns='DT', values='COUNT', aggfunc='sum')
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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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