[英]How to split a date into day, month and year
I have a date in this format - 31Jan2020
, how do I get the day, month and year - 31, Jan and 2020.我有一个这种格式的日期 -
31Jan2020
,我如何获得日、月和年 - 31、Jan 和 2020。
I am looping through a dataframe and the name of the date column is date
.我正在遍历 dataframe 并且日期列的名称是
date
。 This is what I am doing这就是我正在做的
for index,row in data.iterrows():
date = row['date']
print(date)
The code below should work in Python:下面的代码应该在 Python 中工作:
import datetime as dt
import pandas as pd
date_str = '31Jan2020'
a_date = dt.datetime.strptime(date_str, '%d%b%Y').date()
print(a_date.year)
print(a_date.month)
print(a_date.day)
data_df = pd.DataFrame([['31Jan2020'], ['1Jan2020']], columns=['date_str'])
data_df['year'] = data_df['date_str'].map(lambda x: dt.datetime.strptime(x, '%d%b%Y').date().year)
data_df['month'] = data_df['date_str'].map(lambda x: dt.datetime.strptime(x, '%d%b%Y').date().month)
data_df['day'] = data_df['date_str'].map(lambda x: dt.datetime.strptime(x, '%d%b%Y').date().day)
print(data_df)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.