我想撰寫一個程式,從 CSV 格式檔案的 ASCII 表中評估有關建筑工地操作的資料。模板檔案是一個 Excel 檔案。
name Qualification costs
Max Mustermann Seller 6.155,39
Max Mustermann Seller 5.069,15
Max Mustermann Seller 362,08
Klee klumper Seller 4.637,65
Klee klumper Seller 1.159,41
Koch Schnerider Project Engineer 1.358,28
Koch Schnerider Project Engineer 679,14
Müller Manim Distribution 15.149,28
Müller Manim Distribution 16.743,94
Schach Matt Site Manager 14.399,79
Schach Matt Site Manager 1.371,41
Zeimetz Kinder Project Engineer 11.376,50
Zeimetz Kinder Project Engineer 2.133,09
應評估以下資料:
- 所有操作的總成本
- 具有相應費用總額的所有資格
我設法計算了上面這兩個,但我如何管理另外兩個?
- 總成本最高的資格
- 總成本最低的資質
這是我的第一個編碼:
import pandas as pd
import os
filename = "site_operation.csv"
path = "."
file = os.path.join(filename, path)
tscv1 = pd.read_csv(file, sep=";", thousands=".", decimal=",", encoding="ansi")
total_cost = tscv1['costs'].sum()
print("Total costs from all operations: ", total_cost)
uj5u.com熱心網友回復:
您可以使用pandas的groupby函式
for the relative costs, grouped by Qual.
tscv1.groupby('Qualification').sum()
costs
Qualification
Distribution 31893.22
Project Engineer 15547.01
Seller 17383.68
Site Manager 15771.20
# For the min e max values
# an easy way can be sort the results:
sorted_by_qual_value = tscv1.groupby('Qualification').sum().sort_values('costs')
min_qual = sorted_by_qual_value.head(1)
# costs
# Qualification
# Project Engineer 15547.01
max_qual = sorted_by_qual_value.tail(1)
# costs
# Qualification
# Distribution 31893.22
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376574.html
上一篇:為什么創建CSV檔案時有空行?
