将 pandas 数据框中的多个列更改为日期时间 [英] change multiple columns in pandas dataframe to datetime

查看:97
本文介绍了将 pandas 数据框中的多个列更改为日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含13列和55,000行的数据框,我试图将其中的5行转换为日期时间,现在它们返回的是对象类型,我需要转换此数据以进行机器学习,我知道如果我

I have a dataframe of 13 columns and 55,000 rows I am trying to convert 5 of those rows to datetime, right now they are returning the type 'object' and I need to transform this data for machine learning I know that if I do

data['birth_date'] = pd.to_datetime(data[birth_date], errors ='coerce')

它会返回一个datetime列,但我也想对其他4列做一次,是否有一行我可以写给所有人打电话吗?我不认为我可以像

it will return a datetime column but I want to do it for 4 other columns as well, is there one line that I can write to call all of them? I dont think I can index like

data[:,7:12]

谢谢!

推荐答案

您可以使用应用使用 pd.to_datetime

data.iloc[:, 7:12] = data.iloc[:, 7:12].apply(pd.to_datetime, errors='coerce')

这篇关于将 pandas 数据框中的多个列更改为日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆