#-------------------------------------------------------------------- #範例7-9-1:輸出excel檔案,建立單一資料表sheet #【課本14-14頁】 #https://pandas-xlsxwriter-charts.readthedocs.io/ #https://pandas-xlsxwriter-charts.readthedocs.io/chart_examples.html #(1)使用pivot_table,建立樞紐分析表的製作步驟: #df2 = df.pivot_table(columns='上方欄位(x軸)', index='左邊列(y軸)', values='中間顯示欄位', aggfunc='計算函數') #df2 = df.pivot_table(columns='銷售產品', index='業務單位', values='銷售金額', aggfunc='sum') #(2)輸出到excel檔案,指定sheet表格名稱 #格式:df2.to_excel("檔案名稱", sheet_name='sheet_name名稱') #範例:df2.to_excel("score-3a.xlsx", sheet_name='經管3A') #-------------------------------------- #1.練習1:讀取學生成績檔案 #import pandas as pd #df = pd.read_excel("https://acupun.site/lecture/pandas/example/resource/scoreChi-date.xlsx","經管3A") #-------------------------------------- print() print("1-1.顯示全部df =\n") #-------------------------------------- #2.練習2:輸出一般簡單的excel檔案:姓名-各科成績 #df2 = df[['姓名','中文','英文','數學']] #df2.to_excel("score-3a.xlsx", sheet_name='經管3A') #-------------------------------------- print() print('3-1.輸出一般簡單的excel檔案:姓名-各科成績 =\n') #-------------------------------------- #1.練習3:讀取業務銷售分析資料庫 #import pandas as pd #df = pd.read_excel("https://acupun.site/lecture/pandas/example/resource/sales.csv","sales") #-------------------------------------- print() print("3-1.顯示df前5筆 =\n") #-------------------------------------- #4.練習4:輸出樞紐分析表,方法1:用groupby...unstack()建立樞紐分析表(2):3個對應資料:產品(y軸) vs 業務單位(x軸) vs 銷售金額 # df2 = df.groupby(['業務單位','銷售產品'])['銷售金額'].sum().unstack(level='業務單位') # 輸出:df2.to_excel('sales1.xlsx', sheet_name='產品銷售') #-------------------------------------- print() print('3-1.用groupby...unstack()建立樞紐分析表 =\n') print() print('3-2.輸出到excel檔案sales1,產品銷售sheet =\n') #-------------------------------------- #5.練習5:輸出樞紐分析表,方法2:pivot_table法(2個軸【業務單位,銷售產品】,查詢它們的【銷售金額】的總和) # df2 = df.pivot_table(columns='銷售產品', index='業務單位', values='銷售金額', aggfunc='sum') # aggfunc參數:設定values欄位的數學計算函數 = 'sum' ###注意:沒有加() # 輸出:df2.to_excel('sales2.xlsx', sheet_name='產品銷售') #-------------------------------------- print() print('5-1.建立樞紐分析表方法2:pivot_table法(2個軸【業務單位,銷售產品】,查詢它們的【銷售金額】的總和) =\n') print() print('5-2.輸出到excel檔案sales1,產品銷售sheet =\n') #-------------------------------------------------------------------- #-------------------------------------------------------------------- #範例7-9-1:輸出excel檔案,建立單一資料表sheet #【課本14-14頁】 #https://pandas-xlsxwriter-charts.readthedocs.io/ #https://pandas-xlsxwriter-charts.readthedocs.io/chart_examples.html #(1)使用pivot_table,建立樞紐分析表的製作步驟: #df2 = df.pivot_table(columns='上方欄位(x軸)', index='左邊列(y軸)', values='中間顯示欄位', aggfunc='計算函數') #df2 = df.pivot_table(columns='銷售產品', index='業務單位', values='銷售金額', aggfunc='sum') #(2)輸出到excel檔案,指定sheet表格名稱 #格式:df2.to_excel("檔案名稱", sheet_name='sheet_name名稱') #範例:df2.to_excel("score-3a.xlsx", sheet_name='經管3A') #-------------------------------------- #1.練習1:讀取學生成績檔案 #import pandas as pd #df = pd.read_excel("https://acupun.site/lecture/pandas/example/resource/scoreChi-date.xlsx","經管3A") #-------------------------------------- print() import pandas as pd df = pd.read_excel("https://acupun.site/lecture/pandas/example/resource/scoreChi-date.xlsx","經管3A") print("1-1.顯示全部df =\n") #-------------------------------------- #2.練習2:輸出一般簡單的excel檔案:姓名-各科成績 #df2 = df[['姓名','中文','英文','數學']] #df2.to_excel("score-3a.xlsx", sheet_name='經管3A') #-------------------------------------- print() print('3-1.輸出一般簡單的excel檔案:姓名-各科成績 =\n') df2 = df[['姓名','中文','英文','數學']] print(df2) df2.to_excel("score-3a.xlsx", sheet_name='經管3A') #-------------------------------------- #1.練習3:讀取業務銷售分析資料庫 #import pandas as pd #df = pd.read_excel("https://acupun.site/lecture/pandas/example/resource/sales.csv","sales") #-------------------------------------- import pandas as pd df = pd.read_csv("https://acupun.site/lecture/pandas/example/resource/sales.csv") print() print("3-1.顯示df前5筆 =\n", df.head()) #-------------------------------------- #4.練習4:輸出樞紐分析表,方法1:用groupby...unstack()建立樞紐分析表(2):3個對應資料:產品(y軸) vs 業務單位(x軸) vs 銷售金額 # df2 = df.groupby(['業務單位','銷售產品'])['銷售金額'].sum().unstack(level='業務單位') # 輸出:df2.to_excel('sales1.xlsx', sheet_name='產品銷售') #-------------------------------------- print() print('3-1.用groupby...unstack()建立樞紐分析表 =\n') df2 = df.groupby(['業務單位','銷售產品'])['銷售金額'].sum().unstack(level='業務單位') print(df2) print() print('3-2.輸出到excel檔案sales1,產品銷售sheet =\n') df2.to_excel('sales1.xlsx', sheet_name='產品銷售') #-------------------------------------- #5.練習5:輸出樞紐分析表,方法2:pivot_table法(2個軸【業務單位,銷售產品】,查詢它們的【銷售金額】的總和) # df2 = df.pivot_table(columns='銷售產品', index='業務單位', values='銷售金額', aggfunc='sum') # aggfunc參數:設定values欄位的數學計算函數 = 'sum' ###注意:沒有加() # 輸出:df2.to_excel('sales2.xlsx', sheet_name='產品銷售') #-------------------------------------- print() print('5-1.建立樞紐分析表方法2:pivot_table法(2個軸【業務單位,銷售產品】,查詢它們的【銷售金額】的總和) =\n') df2 = df.pivot_table(columns='銷售產品', index='業務單位', values='銷售金額', aggfunc='sum') print(df2) print() print('5-2.輸出到excel檔案sales1,產品銷售sheet =\n') df2.to_excel('sales2.xlsx', sheet_name='產品銷售') #--------------------------------------------------------------------