Python Forum
Filtering by city /Question by beginner
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filtering by city /Question by beginner
#1
Hi there,
I have a task:

I have 3 different csv files (chicago.csv, ney_york.csv and washington csv.) All three files have the same columns with different data. Now I want to ask user (using python) which state do she/he want to see?

Example:
'Would you like to see data for Chicago, New York, or Washington?'

The user's answer:
Chicago

If the user chooses one state, that is mean the files will be filtered in the background for this state:

This is my code:
import time
import pandas as pd
import numpy as np

CITY_DATA = { 'Chicago': 'chicago.csv',
              'New York city': 'new_york_city.csv',
              'Washington': 'washington.csv' }
def get_filters():
cities = ('Chicago', 'New York', 'Washington')
    city = None
    while city not in cities:
        city = input('Would you like to see data for Chicago, New York, or Washington? ')
    def load_data(city):
print(CITY_DATA)
    df = pd.read_csv(CITY_DATA[city])

    return df
def main():
    while True: city
        #For weekday
    df = load_data(city)
And now I got stuck: I don't know how to write filtering for cities? Any ideas?
Reply
#2
indentation is mess line 13,14,15.
get_filters() function dos not make sense.
It shall be one blank line between functions.
Can be written like this.
import time
import pandas as pd
import numpy as np

CITY_DATA = {
    'Chicago': 'chicago.csv',
    'New York city': 'new_york_city.csv',
    'Washington': 'washington.csv'
    }

def city_choice():
    city = input('Would you like to see data for Chicago, New York, or Washington? ')
    return CITY_DATA.get(city, 'No data')

def load_data(city_choice):
    city = city_choice()
    if city == 'No data':
        return 'No city data for input given'
    else:
        df = pd.read_csv(city)
        return df.head()

if __name__ == '__main__':
     print(load_data(city_choice))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Weather info using user api and city id Vimala 9 5,568 Jun-15-2018, 08:30 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020