我想撰寫一個程式,從 CSV 格式檔案的 ASCII 表中評估有關建筑工地操作的資料。知道我想列印出成本最高的資格和成本最低的資格。它應該列印出資格名稱和費用。
模板檔案是一個 Excel 檔案:
name Qualification Month costs
Max Mustermann Seller Dec 18 6.155,39
Max Mustermann Seller Jan 19 5.069,15
Max Mustermann Seller Dec 18 362,08
Klee klumper Seller Jan 19 4.637,65
Klee klumper Seller Mar19 1.159,41
Koch Schnerider Project Engineer Jan 19 1.358,28
Koch Schnerider Project Engineer Jul 19 679,14
Müller Manim Distribution Sep 19 15.149,28
Müller Manim Distribution Jan 19 16.743,94
Schach Matt Site Manager Sep19 14.399,79
Schach Matt Site Manager Jan 19 1.371,41
Zeimetz Kinder Project Engineer Jul 19 11.376,50
Zeimetz Kinder Project Engineer Jan 19 2.133,09
最后它應該是這樣的:
Min. Cost is = Seller 362,08
Max. Cost is = Distribution 16.743,94
我有最小值和最大值,但我如何得到它屬于哪個資格?
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)
minimum = tscv1['costs'].min()
maximum = tscv1['costs'].max()
print("Min. Cost is =", minimum)
print("Min. Cost is =", maximum)
uj5u.com熱心網友回復:
您可以使用nsmallest和nlargest回傳到資料集的頂部/底部 n 行。
# These functions return a single-row data frame.
# We only care about the first row
min_row = df.nsmallest(1, 'costs').iloc[0]
max_row = df.nlargest(1, 'costs').iloc[0]
print(f"Min Cost is = {min_row['Qualification']} {min_row['costs']}")
print(f"Max Cost is = {max_row['Qualification']} {max_row['costs']}")
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376572.html
上一篇:從csv檔案串列創建單個資料幀
下一篇:為什么創建CSV檔案時有空行?
