首页 > 解决方案 > Python:在excel的特定列中重新排列日期

问题描述

我有一个 Excel 表,其中 F 列是日期列表。目前结构如下:

Year-Month-Day Hours:Mins:Secs

 - 2014-02-10 10:57:11
 - 2014-07-11 17:43:07
 - .... ect

我希望它是:

Month Date Year

 - Feb 11 2014
 - Jul 11 2014
 - .... ect

关于如何解决这个问题的任何建议?

加载excel文件->仅编辑F列->保存并用新列替换F列。

标签: pythonexcelpandasdataframe

解决方案


利用dt.strftime

#first cast column to pandas datetime.
#df['ColumnF'] = pd.to_datetime(df['ColumnF'])
df['ColumnG'] = df['ColumnF'].dt.strftime('%b %d %Y')

              ColumnF      ColumnG
0 2014-02-10 10:57:11  Feb 10 2014
1 2014-07-11 17:43:07  Jul 11 2014

解释。

%b: 月份作为语言环境的缩写名称。

%d: 以零填充十进制数表示的月份中的日期。

%Y: 以世纪为十进制数的年份。


推荐阅读