-1

If I've a sheet has a data like these:

Start Time,End Time,Trip Duration,Start Station,End Station,User Type,Gender,Birth Year
2017-05-29 18:36:27,2017-05-29 18:49:27,780,Columbus Dr & Randolph St,Federal St & Polk St,Subscriber,Male,1991.0
2017-06-12 19:00:33,2017-06-12 19:24:22,1429,Kingsbury St & Erie St,Orleans St & Merchandise Mart Plaza,Customer,,
2017-02-13 17:02:02,2017-02-13 17:20:10,1088,Canal St & Madison St,Paulina Ave & North Ave,Subscriber,Female,1982.0

by using pandas I need to load file.csv into a dataframe & find the most frequent hour when people start traveling despite of there isn't an hour column in this dataset!

import pandas as pd

filename = 'file.csv'

df = pd.read_csv('file.csv')

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

df['hour'] = df['Start Time'].dt.hour

popular_hour = 
    
print('Most Frequent Start Hour:', popular_hour)
Klaus D.
  • 13,874
  • 5
  • 41
  • 48

1 Answers1

0

You want the mode.

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

df['hour'] = df['Start Time'].dt.hour

popular_hours = df['hour'].mode()

Note that the returned series will contain multiple values in the case of a tie.

jprebys
  • 2,469
  • 1
  • 11
  • 16