Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Indentationerror
#1
Hi there
I have followed error:
Indentationerror: unindent does not match any outer indentation level


It is all about this line: if weekday != 'all': and this one: if month != 'all':

in this statement:
df = pd.read_csv(CITY_DATA[city])
def main():
    while True: city, month, weekday
        #For weekday
    df = load_data(city, month, weekday)
        # TO DO: display the most common month
     #convert start time into datetime
    df['Start Time'] = pd.to_datetime(df['Start Time'])
    #create a month column
    df['month'] = df['Start Time'].dt.month

    df['day_of_week'] = df['Start Time'].dt.weekday_name
    if weekday != 'all':
        # filter by day of week to create the new dataframe
        df = df[df['day_of_week'] == weekday.title()]
            
             # 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
if __name__ == "__main__":
    main()
How can I fix this?
Reply
#2
The next if statement is not indented properly - if month != 'all':
Move it to the left one space.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
I did:
df['month'] = df['Start Time'].dt.month

    df['day_of_week'] = df['Start Time'].dt.weekday_name
        if weekday !='all':
        # filter by day of week to create the new dataframe
            df = df[df['day_of_week'] == weekday.title()]
            
             # 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
now I have an error here: if weekday !='all':
Error Message: Unexpected indent

ok I got it. Thank you!
Reply
#4
What was the issue?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
moving the if statement to the left :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndentationError: unexpected indent dee 3 2,229 May-02-2022, 02:15 AM
Last Post: dee
  error "IndentationError: expected an indented block" axa 4 2,860 Sep-08-2020, 02:09 PM
Last Post: ibreeden
  IndentationError: unexpected indent jk91 1 2,339 Feb-27-2020, 08:56 PM
Last Post: buran
  IndentationError jagannath 1 2,437 Nov-04-2019, 07:41 AM
Last Post: buran
  IndentationError: expected an indented block ryder5227 2 2,562 Sep-27-2019, 06:59 PM
Last Post: jefsummers
  IndentationError on installed package evvvonder 3 2,975 Jun-29-2019, 10:30 PM
Last Post: Gribouillis
  how do i fix this problem - IndentationError? GrandMaster101 8 7,591 Apr-04-2019, 09:38 PM
Last Post: carloszoom3000
  IndentationError message could be confusing to new programmers insearchofanswers87 1 2,307 May-16-2018, 05:05 PM
Last Post: Larz60+
  IndentationError: unexpected indent (Python) segs 8 17,131 Aug-11-2017, 03:13 PM
Last Post: segs

Forum Jump:

User Panel Messages

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