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


Messages In This Thread
Simple If Statement doesn't work - by Jack_Sparrow - May-14-2018, 09:01 AM
RE: Simple If Statement doesn't work - by j.crater - May-14-2018, 09:10 AM

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