[英]i'm having a bit of a funny problem with Python
我還是 Python 和 Pandas 的新手,但我在這里遇到了一些有趣的問題......當我嘗試獲取輸出時,它給了我一個非常空的 DataFrame。 這有點好笑。
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' }
months=['January','Febraury','March', 'April','May','June','all']
days=['Sunday','Monday','Tuesday','wedensday','Thrusday','Friday','Saturday','all']
print('Hello! Let\'s explore some US bikeshare data!')
def get_filters():
while True:
try:
city =(input('Please enter the name of the city? '))
if city in CITY_DATA:
break
else:
print('This is wrong')
except ValueError:
print("please enter the correct value")
while True:
try:
month=str(input('Please enter the name of Month? '))
if month.title() in months:
break
else:
print('Please enter the correct Month ')
except ValueError:
print("Please don't enter a number or a special character!")
while True:
try:
day=str(input('Please enter the name of day? '))
if day.title() in days:
break
else:
print('please enter the correct day')
except ValueError:
print('Please enter the correct value')
print('-'*40)
return city, month, day
print(get_filters())
def load_data(city, month, day):
"""
Loads data for the specified city and filters by month and day if applicable.
Args:
(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
Returns:
df - Pandas DataFrame containing city data filtered by month and day
"""
df = pd.read_csv(CITY_DATA[city])
# convert the Start Time column to datetime
df['Start Time'] = pd.to_datetime(df['Start Time'])
# extract month, day of week, hour from Start Time to create new columns
df['month'] = df['Start Time'].dt.month
df['day']= df['Start Time'].dt.day_name
# filter by month if applicable
if month != 'all':
# use the index of the months list to get the corresponding int
months = ['january', 'february', 'march', 'april', 'may', 'june']
month = months.index(month) + 1
# filter by month to create the new dataframe
df = df[df['month'] == month]
# filter by day of week if applicable
if day != 'all':
# filter by day of week to create the new dataframe
df = df[df['day'] == day.title()]
return df
def main():
while True:
city, month, day = get_filters()
df = load_data(city, month, day)
print(df.head())
time_stats(df)
station_stats(df)
trip_duration_stats(df)
user_stats(df)
restart = input('\nWould you like to restart? Enter yes or no.\n')
if restart.lower() != 'yes':
break
if __name__ == "__main__":
main()
這輸出:
Empty DataFrame
Columns: [Unnamed: 0, Start Time, End Time, Trip Duration, Start Station, End Station, User Type, Gender, Birth Year, month, day]
Index: []
這是錯誤的,因為我根本沒有得到任何數據。
PS,請原諒我的新手編碼,我仍在學習,我正在做的大部分事情都是自學的.. 在此先感謝
此問題是由於以下原因導致代碼無法根據值進行過濾造成的:
df['day']= df['Start Time'].dt.day_name
它應該是這樣的:
df['day']= df['Start Time'].dt.day_name()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.