[英]Filter column value from multiple csv files using python(pandas)
CITY_DATA = { 'chicago': 'chicago.csv','new york city': 'new_york_city.csv','washington': 'washington.csv' } CITY_DATA = {'芝加哥':'chicago.csv','纽约市':'new_york_city.csv','华盛顿':'washington.csv'}
Asks user to specify a city, month, and day to analyze. 要求用户指定要分析的城市,月份和日期。
Returns:
(str) city - name of the city to analyze
(str) month - name of the month to filter by, or "all" to apply no month filter
(str) day - name of the day of week to filter by, or "all" to apply no day filter
To DO:get user input for city (chicago, new york city, washington). 要执行的操作:获取城市(芝加哥,纽约,华盛顿)的用户输入。 HINT: Use a while loop to handle invalid input
提示:使用while循环处理无效输入
Here is top 5 rows of chicago below which is one of 3 csv file. 这是芝加哥的前5行,其下是3个csv文件之一。 My question is there are total 3 csv files for each city.
我的问题是每个城市总共有3个csv文件。 How can I filter column value(city/month/day) from different files?
如何从不同文件中过滤列值(城市/月/日)? If using loop, it seems wrong that I use if, elif,elif.... for all cities or 12 months or 7 days.
如果使用循环,则在所有城市或12个月或7天使用elif,elif ....似乎是错误的。 Sorry, I am new to Python and I am dizzy for this.
抱歉,我是Python的新手,对此我感到头晕。 Please help with a answer or a hint.
请提供答案或提示。 Thanks.
谢谢。
Start Time End Time Trip Duration \
0 2017-05-29 18:36:27 2017-05-29 18:49:27 780
1 2017-06-12 19:00:33 2017-06-12 19:24:22 1429
2 2017-02-13 17:02:02 2017-02-13 17:20:10 1088
3 2017-04-24 18:39:45 2017-04-24 18:54:59 914
4 2017-01-26 15:36:07 2017-01-26 15:43:21 434
Start Station End Station \
0 Columbus Dr & Randolph St Federal St & Polk St
1 Kingsbury St & Erie St Orleans St & Merchandise Mart Plaza
2 Canal St & Madison St Paulina Ave & North Ave
3 Spaulding Ave & Armitage Ave California Ave & Milwaukee Ave
4 Clark St & Randolph St Financial Pl & Congress Pkwy
User Type Gender Birth Year
0 Subscriber Male 1991.0
1 Customer NaN NaN
2 Subscriber Female 1982.0
3 Subscriber Male 1966.0
4 Subscriber Female 1983.0
What is wrong with the code below? 以下代码有什么问题? Should place city=input('Enter a city') after if statement?
是否应该在if语句后放置city = input('Enter a city')? confused.
困惑。
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_city():
print("Hello! Let's explore some US bikeshare data! \n Which city would you like? \n Chicago, New York City or Washington? ")
cities = ['chicago', 'new york city', 'washington']
city = input('Enter a city: ')
Enter a city: san jose
if city == 'chicago':
return chicago
elif city == 'new york city':
return new_york_city
elif city == 'washington':
return washington
else:
print ('Ops, your enter is out of range.')
File "<ipython-input-14-335bd5bdf8dc>", line 2
return chicago
^
SyntaxError: 'return' outside function
This is the data analysis project for Udacity. 这是Udacity的数据分析项目。 This is how I did it.
这就是我做的。
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():
"""
Asks user to specify a city, month, and day to analyze.
Returns:
(str) city - name of the city to analyze
(str) month - name of the month to filter by, or "all" to apply no month filter
(str) day - name of the day of week to filter by, or "all" to apply no day filter
"""
print('Hello! Let\'s explore some US bikeshare data!')
cities = ['chicago', 'new york city', 'washington']
cond = 0
while cond != 1:
city = input("Which city would you like? ")
city = city.lower()
if city in cities:
cond = 1
else:
cond = 0
# TO DO: get user input for month (all, january, february, ... , june)
months = ['all' , 'january', 'february', 'march','april','may','june','july','august','september''october','november','december']
cond = 0
while cond != 1:
month = input("Which month would you like? ")
month = month.lower()
if month in months:
cond = 1
else:
cond = 0
# TO DO: get user input for day of week (all, monday, tuesday, ... sunday)
days = ['all', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday','sunday']
cond = 0
while cond != 1:
day = input("Which day would you like? ")
day = day.lower()
if day in days:
cond = 1
else:
cond = 0
print('-'*40)
return city, month, day
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.