chevron_left
Time Series Cookbook
Adding new column containing the difference between two date columnsCombining columns containing date and timeCombining columns of years, months and daysConverting a column of strings to datetimeConverting dates to stringsConverting datetime column to date and time columnsConverting DatetimeIndex to Series of datetimeConverting index to datetimeConverting UNIX timestamp to datetimeCreating a column of datesCreating a range of datesExtracting month and year from Datetime columnGetting all weekdays between two datesGetting all weekends between two datesGetting day unit from date columnGetting month unit from date columnGetting name of months in date columnGetting week numbers from a date columnGetting day of week of date columnsGetting year unit from date columnModifying datesOffsetting datetimeRemoving time unit from datesSetting date to beginning of monthSorting DataFrame by datesUsing dates as the index of a DataFrame
check_circle
Mark as learned thumb_up
2
thumb_down
2
chat_bubble_outline
0
auto_stories new
settings
Combining columns of years, months and days in Pandas
Pandas
chevron_rightCookbooks
chevron_rightDataFrame Cookbooks
chevron_rightTime Series Cookbook
schedule Jul 1, 2022
Last updated local_offer Python●Pandas
Tags tocTable of Contents
expand_more Check out the interactive map of data science
Consider the following DataFrame:
df
Year Month Day0 2020 10 251 2020 11 26
Solution
To combine the Year
, Month
and Day
columns to form another column:
df
Year Month Day Date0 2020 10 25 2020-10-251 2020 11 26 2020-11-26
Here, column Date
is of type datetime
.
Explanation
We first combine Year
, Month
and Day
columns via simple string concatenation:
df["Year"] + "/" + df["Month"] + "/" + df["Day"]
0 2020/10/251 2020/11/26dtype: object
Here, the combined column (Series
) is of type string
. We can convert its data type to datetime
using pd.to_datetime(~)
:
0 2020-10-251 2020-11-26dtype: datetime64[ns]
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...
thumb_up
2
thumb_down
2
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!