python - 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列。
解决方案
利用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
: 以世纪为十进制数的年份。
推荐阅读
- arrays - 带有对象数组的 HTTPS 发布 URL
- javascript - React - useMemo 有一个复杂的表达式 - 将其提取到一个单独的变量中,以便对其进行静态检查
- perl - 如何获取 DateTime::Format::Strptime 来解析闰秒的日期/时间?
- javascript - 如何使 AppBar 背景颜色与导航栏颜色一致
- machine-learning - 如何在前馈神经网络中使用三通道图像?
- javascript - 如何禁用 Bootstraps 按钮复选框悬停和焦点效果?
- javascript - 如何在客户端渲染之前清理 HTML 输入值,我们可以过滤允许的标签
- python - 用pickle加载一个类的实例?
- python - 如何使用队列实现堆栈接口?
- docker - 如何调试 TLS/SSL 连接