Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

Convert a date format with month name into proper date with pandas

I have a date column having values like 'November 4, 2016'

Can we convert it into proper date using pandas like this format '4/11/16'

using pandas to_datetime or any other funtion.

question from:https://stackoverflow.com/questions/66058307/convert-a-date-format-with-month-name-into-proper-date-with-pandas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Consider df:

In [463]: df = pd.DataFrame({'date': ['November 4, 2016', 'December 1, 2017']})

In [464]: df
Out[464]: 
               date
0  November 4, 2016
1  December 1, 2017

Use pd.to_datetime with Series.dt.strftime:

In [469]: df['date'] = pd.to_datetime(df['date'], format='%B %d, %Y').dt.strftime('%d/%m/%y')

In [479]: df
Out[479]: 
       date
0  04/11/16
1  01/12/17

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...