繁体   English   中英

如何找出这张 excel 表中的哪一行数据重复最多

[英]How can I find out which row of data from this excel sheet is duplicated the most

我试图找出 excel 电子表格中哪一行(街道名称)的犯罪率最高。 我找到了最高数量的犯罪总和,我只是找不到产生这么多事件的实际行。

进口操作系统进口csv

def main(): #创建并保存文件的路径... fileToRead = "C:/Users/zacoli4407/Documents/Intro_To_Scipting/Crime_Data_Set.csv"

highestNumberOfCrimes = 0
data = []
rowNumber = 0
count = 0

with open(fileToRead, 'r') as dataToRead:
    dataToRead = open(fileToRead, 'r') # open the access to the file
    reader = csv.reader(dataToRead) # gives the ability to read from the file

    for row in reader:
        if row[4].isnumeric():
            if int(row[4]) > highestNumberOfCrimes:
                highestNumberOfCrimes = int(row[4])
                rowNumber = count
            data.append([row[2],row[3],row[4],row[5]]) #row 3 has the street name I am trying to acquire
            count += 1
            
print(highestNumberOfCrimes)
with open("crime.txt", "w") as outputFile:
    outputFile.write("The highest number of crimes is: \n")
    outputFile.write(str(highestNumberOfCrimes))

                

主要的()

您可以执行以下操作:

import csv
from collections import defaultdict

result = defaultdict(float)
with open(fileToRead, 'r') as dataToRead:
    reader = csv.reader(dataToRead)
    header = next(reader)
    for  row in reader:
        result[row[3]]  += float(row[4])

#Now to get the street with maximum number of crimes
mx = max(result, key = result.get)
print(mx)

#to get the maximum number of crimes
print(result[mx])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM