我之前問過這個問題,但答案不是作為函式提供的。我試圖加入一個 dunction 但它沒有用所以我再問一次:)
所以這是我必須分析的示例 CSV 檔案
1,8dac2b,ewmzr,jewelry,phone0,9759243157894736,us,69.166.231.58,vasstdc27m7nks3
2,668d39,aeqok,furniture,phone1,9759243157894736,jp,50.201.125.84,jmqlhflrzwuay9c
3,622r49,arqek,vehicle,phone2,9759544365415694736,az,53.001.135.54,weqlhrerreuert6f
4,6444t43,rrdwk,vehicle,phone9,9759543263245434353,au,54.241.234.64,weqqyqtqwrtert6f
我def popvote(list)嘗試使用此函式回傳csv 串列中每個串列的第四行中最受歡迎的內容,在上面的示例中是vehicle.
下面解釋
這是我到目前為止
def popvote(list):
for x in list:
g = list(x)
if x = max(g[x]):
return x
然而,這并沒有真正起作用..我應該改變什么以確保它有效?
注意:答案應該作為一個集合回傳
說明:所以我試圖根據下面 (** xxxx **) 中指示的內容回傳串列中重復次數最多的值
1,8dac2b,ewmzr,**jewelry**,phone0,9759243157894736,us,69.166.231.58,vasstdc27m7nks3
2,668d39,aeqok,**furniture**,phone1,9759243157894736,jp,50.201.125.84,jmqlhflrzwuay9c
3,622r49,arqek,**vehicle**,phone2,9759544365415694736,az,53.001.135.54,weqlhrerreuert6f
4,6444t43,rrdwk,**vehicle**,phone9,9759543263245434353,au,54.241.234.64,weqqyqtqwrtert6f
所以在這種情況下,車輛應該是輸出。
uj5u.com熱心網友回復:
原始 python 方法,使用collections.Counter:
import csv
from collections import Counter
def read_categories():
with open("tmp.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
yield row[3]
counter = Counter(read_categories())
counter.most_common(n=1)
# [('vehicle', 2)]
僅限原始蟒蛇:
import csv
value_to_count = {}
with open("tmp.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
category = row[3]
if category in value_to_count:
value_to_count[category] = 1
else:
value_to_count[category] = 1
# sorted list of counts and values
count_to_value = sorted((v, k) for k, v in value_to_count.items())
if count_to_value:
print("most common", count_to_value[-1])
# most common (2, 'vehicle')
如果您發現convtools有用,那么:
from convtools import conversion as c
from convtools.contrib.tables import Table
rows = Table.from_csv("tmp.csv", header=False).into_iter_rows(tuple)
# this is where code generation happens, it makes sense to store
# the converter in a separate variable for further reuse
converter = c.aggregate(c.ReduceFuncs.Mode(c.item(3))).gen_converter()
converter(rows)
# "vehicle"
uj5u.com熱心網友回復:
正如評論所指出的,您可以使用df.mode()和型別轉換來設定結果。
df = pd.read_csv("filename.csv", header=None)
set(df[3].mode())
Out: {'vehicle'}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/375892.html
上一篇:雙回圈串列理解
