I want to have the user input a name that I have in a dictionary that is in row 3
. The program will look through the dictionary in row 3
to find the word and count how many times the word appears on a separate line.
I also would like to program to find all the zip codes this type of tree is found in and the city that has the greatest number of this type of tree.
Enter a command: treeinfo white oak
Entry: white oak
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
Here is what I have so far, it ask me to enter tree name and nothing happens then it loops as ask me to enter tree name again.
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)
What i see when I run the program:
Enter tree species:mulberry
Enter tree species:
I'm still a beginner so maybe someone can catch what i'm doing wrong here.
I tried to swap
with open('nyc_street_trees.csv','r') as my_file:
data = my_file.read()
with
inputFile = open("data.txt", "r")
I tried
count = 0
for key in tree_type:
if tree_type[spc_common] == 'data':
count = 1
CodePudding user response:
The easiest way to import and manipulate data from a CSV is from the pandas library.
- Import the CSV file into a dataframe
- Extract relevant information from the dataframe
As you have not provided the headers for the CSV, I have used row and column index notation to retrieve the data. I have made the assumption that the columns you used are correct. It would be safer to use the column header names, but the following code should work nonetheless.
df.iloc[:,3]
anddf.columns[3]
= refers to the spc_common columndf.iloc[:,4]
= refers to the zipcode columndf.iloc[:,5]
anddf.columns[5]
= refers to the borough column
Code:
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]}")