python-3.x - 如何在pandas中将日期列转换为日期、月份和年份格式的三列?
问题描述
我想将给定的日期列转换为日期、月份和年份格式。最初,转换后有 2 列,它将是 4 列
Country|Date|Month|Year
给定的数据框的类型
test=pd.DataFrame({'Date':['2014,1,1','2014,4,17'],'Country':['Denmark','Australia']})
解决方案
熊猫有一个to_datetime
功能。
import pandas as pd
df = pd.DataFrame({'Date':['2014,1,1','2014,4,17']})
df["Date"] = pd.to_datetime(df["Date"], format="%Y,%m,%d")
# If you want to save other datetime attributes as their own columns
# just pull them out assign them to their own columns
# df["Month"] = df["Date"].dt.month
# df["Year"] = df["Date"].dt.year
推荐阅读
- sql - 查找点类型的最高出现次数
- arrays - Array Map多维,从3个数组Java中获取值
- javascript - 如果给定出生日期,自动更改出生日期?
- javascript - 这两个字符串的区别(JavaScript)
- matlab - MATLAB 中的序列数值积分
- linux - Ubuntu 上的 OpenFOAM。符号查找错误:surfaceFeatureExtract:未定义符号
- css - React:将 CSSProperties 转换为 Styled-Component
- javascript - 如何在表中的每一行中创建动态依赖下拉列表而不影响laravel中的任何行
- swiftui - 选择特定视图时如何使用生命周期方法?
- asp.net-core - 在父项的提交操作中获取部分视图模型