gpt4 book ai didi

python - 无法更改 pandas 中的日期时间

转载 作者:太空宇宙 更新时间:2023-11-03 16:43:56 24 4
gpt4 key购买 nike

我有以下 csv 数据:

sep=;
ACCELEROMETER X (m/s²);ACCELEROMETER Y (m/s²);ACCELEROMETER Z (m/s²);GRAVITY X (m/s²);GRAVITY Y (m/s²);GRAVITY Z (m/s²);LINEAR ACCELERATION X (m/s²);LINEAR ACCELERATION Y (m/s²);LINEAR ACCELERATION Z (m/s²);GYROSCOPE X (rad/s);GYROSCOPE Y (rad/s);GYROSCOPE Z (rad/s);LIGHT (lux);MAGNETIC FIELD X (μT);MAGNETIC FIELD Y (μT);MAGNETIC FIELD Z (μT);ORIENTATION Z (azimuth °);ORIENTATION X (pitch °);ORIENTATION Y (roll °);PROXIMITY (m);ATMOSPHERIC PRESSURE (hPa);TEMPERATURE (C);RELATIVE HUMIDITY (%);SOUND LEVEL (dB);LOCATION Latitude : ;LOCATION Longitude : ;LOCATION Altitude ( m);LOCATION Altitude-google ( m);LOCATION Altitude-atmospheric pressure ( m);LOCATION Speed ( m/s);LOCATION Accuracy ( m);LOCATION ORIENTATION (°);Satellites in range;Time since start in ms ;YYYY-MO-DD HH-MI-SS_SSS
0.2825;0.3932;10.0299;-0.219;2.4781;9.4859;0.5052;-2.0443;0.5388;0.0699;0.0215;0.0045;18.0;43.3;-52.23;64.99;213.54;-15.07;1.18;8.0;1023.4;23.0;63.3;61.013;32.06071;34.775364;;29.3243923;;;30.0;;0 / 0;6;2015-12-07 20:51:06:608
0.6422;-0.5429;10.076;-0.023;1.4988;9.6914;0.7552;-1.3062;-0.0186;0.0046;0.0582;0.1172;19.0;43.6;-51.87;70.25;215.0;-9.66;-0.18;8.0;1023.37;23.0;63.3;41.656;32.06071;34.775364;;29.3243923;;;30.0;;0 / 0;1007;2015-12-07 20:51:07:609

当我尝试使用该命令时:

data['YYYY-MO-DD HH-MI-SS_SSS'] = pd.to_datetime(data['YYYY-MO-DD HH-MI-SS_SSS'], format='%Y-%m-%d %H:%M:%S:%f')

我收到以下错误:

KeyError: 'YYYY-MO-DD HH-MI-SS_SSS'

出了什么问题?(我是Python新手,但我不明白)

最佳答案

我认为您可以将参数 skiprows=1 添加到 read_csv用于跳过 csv 的第一行:

df = pd.read_csv('university.csv', sep=";", skiprows=1)

然后你可以转换 to_datetime :

df['YYYY-MO-DD HH-MI-SS_SSS'] = pd.to_datetime(df['YYYY-MO-DD HH-MI-SS_SSS'], 
format='%Y-%m-%d %H:%M:%S:%f')

或者您可以使用read_csv使用参数 parse_datesdate_parser:

import pandas as pd

dateparse = lambda x: pd.datetime.strptime(x, '%Y-%m-%d %H:%M:%S:%f')
df = pd.read_csv('university.csv',
sep=";",
skiprows=1,
parse_dates=['YYYY-MO-DD HH-MI-SS_SSS'],
date_parser=dateparse)

print df.head()

关于python - 无法更改 pandas 中的日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36514888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com