python - Multiple columns extracted from one Pandas column -
i have (parsed) datetime column in pandas dataframe. need generate multiple columns based on 1 column, 1 year, 1 month, hour, day of week etcetera. i'm doing number of individual applies large dataset , i'm iterating on df multiple times. there better pattern accomplish this? can apply return dataframe paste behind it?
if dtype
datetime
can use vectorised datetime accessor dt
add columns:
in [11]: df = pd.dataframe({'date':pd.date_range(dt.datetime(2016,1,1), end = dt.datetime(2016,1,10))}) df out[11]: date 0 2016-01-01 1 2016-01-02 2 2016-01-03 3 2016-01-04 4 2016-01-05 5 2016-01-06 6 2016-01-07 7 2016-01-08 8 2016-01-09 9 2016-01-10 in [13]: df['year'],df['month'],df['day'], df['day_of_week'] = df['date'].dt.year, df['date'].dt.month, df['date'].dt.day, df['date'].dt.dayofweek df out[13]: date year month day day_of_week 0 2016-01-01 2016 1 1 4 1 2016-01-02 2016 1 2 5 2 2016-01-03 2016 1 3 6 3 2016-01-04 2016 1 4 0 4 2016-01-05 2016 1 5 1 5 2016-01-06 2016 1 6 2 6 2016-01-07 2016 1 7 3 7 2016-01-08 2016 1 8 4 8 2016-01-09 2016 1 9 5 9 2016-01-10 2016 1 10 6
Comments
Post a Comment