我是 Python 新手。我有一個我正在決議的 CSV 檔案,并希望回傳滿足兩個條件的資訊。
資料包含有關消費金融產品和服務的投訴。檔案中有 16 列,我希望滿足其中兩個列的條件,即 ['Product'] 和 ['Timely Response]。我希望獲得對最常發生的 ['Product'] 的及時回應的百分比。['Product'] 包含 9 個產品,['Timely Response'] 是一個 Yes/No 欄位。
我使用 itemgetter() 來回傳抱怨最多的產品:
for row in reader:
id_counts = Counter(map(itemgetter(1), reader))
pprint (id_counts)
回傳:
Counter({'Credit reporting, credit repair services, or other personal consumer reports': 112,
'Debt collection': 32,
'Mortgage': 12,
'Credit card or prepaid card': 11,
'Checking or savings account': 11,
'Student loan': 5,
'Money transfer, virtual currency, or money service': 4,
'Vehicle loan or lease': 4,
'Payday loan, title loan, or personal loan': 1})
我現在希望計算對最常見投訴的及時回應。
for row in reader:
if row[1] == 'Credit reporting, credit repair services, or other personal consumer reports':
c = Counter(map(itemgetter(15), reader))
print (c)
Counter({'Yes': 186, 'No': 4})
這是不正確的,并且從所有欄位中計算是/否。
我也試過:
for row in reader:
if row[1] == 'Credit reporting, credit repair services, or other personal consumer reports':
c = Counter(row[15].split())
print (sum(c))
它回傳了一個不受支持的運算元錯誤。
我想使用 getitem 或 Counter 來解決這個解決方案,因為這是我開始的,但非常感謝任何幫助/建議。
uj5u.com熱心網友回復:
c = Counter(map(itemgetter(15), reader))
您正在從 讀取所有(剩余)元素reader,但您只想從當前讀取row。
你可以用itemgetter;
c = Counter()
for row in reader:
if row[1] == 'Credit reporting, credit repair services, or other personal consumer reports':
c.update([itemgetter(15)(row)])
print (c)
...但這比顯而易見的要復雜得多
c = Counter({"Yes": 0, "No": 0})
for row in reader:
if row[1] == 'Credit reporting, credit repair services, or other personal consumer reports':
c[row[15]] = 1
print (c)
也許這里的關鍵觀察是你一次只看一排。您的邏輯似乎假設您正在檢查所有匹配的行,但實際上您只是一個接一個地遍歷它們并依次檢查每個行。
...如果你想變得花哨,你可以說
c = Counter(
map(itemgetter(15),
filter(lambda row: row[1] == 'Credit reporting, credit repair services, or other personal consumer reports',
reader)))
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/531440.html
