Python Forum
Simple If Statement doesn't work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple If Statement doesn't work
#1
Hi guys,
I have this column:

Start Time
0 2017-05-29 18:36:27
1 2017-06-12 19:00:33
2 2017-02-13 17:02:02
3 2017-04-24 18:39:45
4 2017-01-26 15:36:07

I want to calculate the Most Frequent Months of Travel.
There are my steps:

#transform strart date into datetime
df['Start Time'] = pd.to_datetime(df['Start Time'])
#generate new column with months
df['month'] = df['Start Time'].dt.month
#calculate the Frequent Months of Travel
popular_month = df['month'].mode()[0]
#rename int into str
df1['month_name']:
    if popular_month = 1:
        return 'January'
    elif popular_month = 6:
        return 'June'
    else:
        return 'Others'
print('\nThe Most Frequent Months of Travel:', month_name)
I have a problem with rename month as an int (1, 2, 3, etc.) into str (January, February etc.)

What is the right if statement here?

Thank you!
Reply
#2
There are much better solutions than using if statement. E.g. put your numbers and corresponding months in a dictionary. Then just access the dictionary value (month string) by key (month number). If you need "others" for undefined months, return the default value.
Reply
#3
good idea!
How can I access the dictionary values?

df['Start Time'] = pd.to_datetime(df['Start Time'])

df['month'] = df['Start Time'].dt.month
popular_month = df['month'].mode()[0]
month_name = [1 = 'January', 2 = 'February', 6 = 'June']

print('\nThe Most Frequent Months of Travel:', month_name)
Reply
#4
you build the dictionary this way:
month_name = {1:'January', 2:'February', 3:'March', 4:'April', 5:'Mai', 6:'June', 7:'July', 8:'August', 9:'September', 10:'October', 11:'November', 12:'December'}
print('\nThe Most Frequent Months of Travel:', month_name[popular_month])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 930 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,778 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 885 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  client.get_all_tickers() Doesn't work gerald 2 1,705 Jun-16-2022, 07:59 AM
Last Post: gerald
  pip doesn't work after Python upgrade Pavel_47 10 4,182 May-30-2022, 03:31 PM
Last Post: bowlofred
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 2,007 Dec-18-2021, 02:38 AM
Last Post: knight2000
  getting an import statement to work in my program barryjo 1 1,655 Dec-06-2021, 04:28 PM
Last Post: snippsat
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,699 Oct-30-2021, 11:20 PM
Last Post: Yoriz
  Join dataframes... So simple but I can't work it out! snakes 1 1,361 Oct-27-2021, 09:15 AM
Last Post: snakes
  Process doesn't work but Thread work ! mr_byte31 4 2,610 Oct-18-2021, 06:29 PM
Last Post: mr_byte31

Forum Jump:

User Panel Messages

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