使用python中的pandas将日期分割成单独的年月日时分列
读取数据:
import datetime
from datetime import datetime
import warnings
warnings.filterwarnings(action='ignore')
from matplotlib.pylab import style
style.use('ggplot')
data = pd.read_csv('./年月日时分秒拆分时间列.csv')
data
获取时间列,设置为日期格式:
df = data['Date']
df=pd.to_datetime(df)
df
查看数据类型:
将年月日时分分割出来组成新的单独的列:
def get_ymd(date):
# 这里的输入date是一列年月日数据
Y,M,D,H,m=[],[],[],[],[]
for i in range(len(date)):
oneday=date[i]
year=oneday.year
month=oneday.month
day=oneday.day
hour=oneday.hour
minute=oneday.minute
Y.append(year)
M.append(month)
D.append(day)
H.append(hour)
m.append(minute)
date=pd.DataFrame()
date['year']=Y
date['month']=M
date['day']=D
date['hour']=H
date['minute']=m
return date
调用分隔列函数:
df = get_ymd(df)
df