使用 Pandas 查詢給定 Excel 表格中的損益
Pandas 是 Python 中一個流行的資料操作和分析庫,被資料科學家和分析師廣泛使用。它提供了許多用於處理 Excel 表格中資料的函式。在分析財務資料中最常見的任務之一是在給定的 Excel 表格中查詢損益。
設定
要使用 Python 處理 Excel 檔案,您需要安裝 **openpyxl** 依賴項。為此,請開啟您的終端並輸入以下命令:
pip install openpyxl
安裝成功後,您可以開始嘗試使用 Excel 檔案和電子表格。
要下載以下練習中使用的 Excel 電子表格,請檢視 此連結
演算法
要將 Excel 檔案中的資料讀取到 Pandas DataFrame 中,請使用內置於 Pandas 中的 **read_excel()** 方法。為了計算損益,我們必須從總收入中扣除總成本。可以使用以下步驟總結使用 Pandas 計算損益的演算法:
使用 **read_excel()** 方法將 Excel 表格讀取到 Pandas DataFrame 中。
DataFrame 應該更新一個用於損益的新列。
從總收入中減去總成本以確定每一行的損益。
將 DataFrame 中的損益列加總以確定總的損益。
示例 1
以下程式碼讀取名為 **'sales.xlsx'** 的 Excel 表格並建立一個 DataFrame。然後,它為損益新增一個新列並計算每一行的損益。
import pandas as pd
# read excel sheet into pandas dataframe
df = pd.read_excel('sales.xlsx')
# calculate total cost
df['Total Cost'] = df['Units Purchased'] * df['Unit Cost']
# calculate total revenue
df['Total Revenue'] = df['Units Sold'] * df['Unit Price']
# calculate profit/loss
df['Profit/Loss'] = df['Total Revenue'] - df['Total Cost']
# print the resulting dataframe
print(df)
# save the resulting dataframe to a new excel sheet
df.to_excel('sales_results.xlsx', index=False)
輸出
Units Purchased Unit Cost Units Sold Unit Price Item Name Total Cost Total Revenue Profit/Loss
50 5.00 40 9.00 Apples 250.00 360.0 110.00
100 3.50 80 7.00 Oranges 350.00 560.0 210.00
25 12.00 20 15.00 Pineapples 300.00 300.0 0.00
75 1.75 60 3.50 Bananas 131.25 210.0 78.75
200 0.50 180 1.25 Carrots 100.00 225.0 125.00
450 2.00 120 4.50 Potatoes 900.00 540.0 -360.00
40 8.00 30 12.00 Avocados 320.00 360.0 40.00
80 1.50 70 3.00 Tomatoes 120.00 210.0 90.00
300 20.00 25 25.00 Mangoes 6000.00 625.0 -5375.00
60 4.00 45 8.00 Grapes 240.00 360.0 120.00
在此示例中,我們首先匯入 Pandas 庫,然後使用 **read_excel**() 函式讀取 Excel 表格。然後,我們在資料框中建立新列以計算每個產品的總成本、總收入和損益。最後,我們列印包含新列和計算值的結果資料框,並將其儲存到新的 Excel 表格中以供進一步處理。
示例 2:使用過濾器計算損益
import pandas as pd
# read excel sheet into pandas dataframe
df = pd.read_excel('sales_results.xlsx')
# filter the dataframe to include only the products with profit
df_profit = df[df['Total Revenue'] > df['Total Cost']]
# calculate the total profit
total_profit = df_profit['Total Revenue'].sum() - df_profit['Total Cost'].sum()
# filter the dataframe to include only the products with loss
df_loss = df[df['Total Revenue'] < df['Total Cost']]
# calculate the total loss
total_loss = df_loss['Total Cost'].sum() - df_loss['Total Revenue'].sum()
# print the total profit and loss
print(f"Total Profit: {total_profit}")
print(f"Total Loss: {total_loss}")
輸出
Total Profit: 773.75 Total Loss: 5735.0
首先匯入 **Pandas** 庫,然後使用 read_excel() 函式讀取先前示例中儲存的結果 Excel 表格。然後,我們**過濾**資料框以僅包含有盈利的商品並計算總利潤。類似地,我們過濾資料框以僅包含虧損的商品並計算總虧損。最後,我們使用 print() 函式列印總利潤和虧損。
使用 Pandas 計算損益的應用
**財務資料分析** - 企業可以使用 Pandas 分析其財務資訊並確定各種商品和服務的損益。
**投資分析** - 投資者可以使用 Pandas 分析公司的財務資訊以確定它是否盈利。
**業務預測** - 企業可以透過分析歷史資料使用 Pandas 預測未來的收入和損失。
結論
對於分析和計算 Excel 表格中的損益,Pandas 是一個可以使用的強大的 Python 庫。由於其簡單的介面和強大的功能,Pandas 對於任何資料分析師或財務專家來說都是一個必不可少的工具。開發人員可以透過遵循本文中提供的示例,使用 Pandas 分析其財務資料並深入瞭解其業務的成功情況。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP