我想让用户输入一个名字,这个名字在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
1条答案
按热度按时间6bc51xsx1#
从CSV导入和操作数据的最简单方法是从pandas library导入。
由于您没有提供CSV的标题,我使用了行和列索引符号来检索数据。我假设您使用的列是正确的。使用列标题名称会更安全,但下面的代码仍然可以工作。
df.iloc[:,3]
和df.columns[3]
=引用spc_common列df.iloc[:,4]
=引用邮政编码列df.iloc[:,5]
和df.columns[5]
=指区列代码: