csv 用户输入通读词典,查找单词的出现次数

biswetbf  于 2022-12-15  发布在  其他
关注(0)|答案(1)|浏览(132)

我想让用户输入一个名字,这个名字在row 3的字典里,程序将在row 3的字典里查找这个单词,并计算这个单词在单独一行中出现的次数。
我还想程序找到所有的邮政编码这种类型的树是在和城市,有最大数量的这种类型的树。

输入命令:白色栎树
入口:白色橡木

Total number of such trees: 642
Zip codes in which this tree is found: 10011,11103,11375,10002,10463
Borough containing the largest number of such trees: Bronx, with 432

这是我到目前为止所拥有的,它要求我输入树名,但没有发生任何事情,然后它循环要求我再次输入树名。

import csv
from collections import Counter
# variables
# Load data into string variable
# file is variable called data
with open('nyc_street_trees.csv','r') as my_file:
     data = my_file.read()

# Create a list of each row
rows = data.split("\n")

# Create a dictionary
tree_type = {}

# Iterate through each row, this prints out each row on a separate line
for row in rows:
    # Split the row into info
    info = row.split(",")

    # Check if the row is the proper length
    if len(info) == 9:
        # separate out the data
        tree_id = info[0]
        tree_dbh = info[1]
        health = info[2]
        spc_common = info[3]
        zipcode = info[4]
        boroname = info[5]
        nta_name = info[6]
        latitude = info[7]
        longitude = info[8]

        # Populate the dictionary
        tree_type[spc_common] = info[3]

        userinput = input('Enter tree species:')

count = 0
for key in tree_type:
    if tree_type[spc_common] == 'userinput':
        count += 1

print(count)

#print(tree_type)
        # prints out the row with tree names
       # print(f'Spc: {spc_common} ')
#print(data)
#print(rows)
  # print(row)
#print(type(rows))
  # print(type(info), info)

运行程序时我看到的内容:

Enter tree species:mulberry
Enter tree species:

我还是个初学者,所以也许有人能发现我做错了什么。
我想换

with open('nyc_street_trees.csv','r') as my_file:
     data = my_file.read()

inputFile = open("data.txt", "r")

我试过了

count = 0
for key in tree_type:
    if tree_type[spc_common] == 'data':
        count += 1
6bc51xsx

6bc51xsx1#

从CSV导入和操作数据的最简单方法是从pandas library导入。

  • 将CSV文件导入 Dataframe
  • 从 Dataframe 中提取相关信息

由于您没有提供CSV的标题,我使用了行和列索引符号来检索数据。我假设您使用的列是正确的。使用列标题名称会更安全,但下面的代码仍然可以工作。

  • df.iloc[:,3]df.columns[3] =引用spc_common
  • df.iloc[:,4] =引用邮政编码
  • df.iloc[:,5]df.columns[5] =指

代码:

import pandas as pd

# Import the data from CSV file
df = pd.read_csv('nyc_street_trees.csv')

# Get user to enter the tree species
userinput = input("Enter tree species:")

# Find the number of trees of that species
number_of_trees = df.iloc[:,3][df.iloc[:,3] == userinput].count()

# Find the zip codes containing that species
zip_codes = ",".join(map(str, list(set(df.iloc[:,4][df.iloc[:,3] == userinput]))))

# Find the borough containing the most number of trees of that species
borough_with_most_trees = df.iloc[:,[3,5]][df.iloc[:,3] == userinput].groupby(df.columns[5]).count().nlargest(1, columns=(df.columns[3]))

# Display output
print(f"Total number of {userinput} trees: {number_of_trees}")
print(f"Zip codes in which {userinput} trees are found: {zip_codes}")
print(f"Borough containing the largest number of {userinput} trees: {borough_with_most_trees.index[0]}, with {borough_with_most_trees.iloc[0,0]}")

相关问题