读取cvs文件后如何在数据框中添加时间戳记?我有一个带有测量的数据集,但是没有时间戳。我知道传感器数据的频率(200 Hz)和开始日期/时间。
我试图计算文件中的行数并创建一个时间列。使用pd.insert我插入了此时间戳。我的问题是,在绘制这些数据时,我的x轴未显示归因于时间戳,而是显示了测量次数。我的代码:
#Importing signals
data = pd.read_csv('.../monday.txt')
data.columns = ['l1','l2','l3','l4','l5','l6']
print("Sensor data: ")
print(data.head())
print(data.dtypes)
nbrMeasurments = sum(1 for line in open('.../monday.txt'))
data.insert(0, "Time", pd.timedelta_range('11:24:26',
periods=nbrMeasurments-1, freq="5L"))
print("Revised sensor data: ")
print(data.head())
print(data.dtypes)
在另一个有时间戳记的文件中,pd.read_csv('.../mondayV1.csv',index_col='Date', usecols= [0,1,2], parse_dates=True)
中的“ index_col ='Date'”看起来像是确保x轴按日期而不是测量编号“ x”引用的命令”:
SYS (mmHg) DIA (mmHg)
Date
2019-08-07 13:06:30 111 61
2019-08-07 13:07:08 114 64
2019-08-07 13:07:56 112 63
2019-08-07 13:08:42 127 81
2019-08-07 13:09:19 129 83
Omron data types:
SYS (mmHg) int64
DIA (mmHg) int64
在尝试插入没有时间戳的文件时,“时间”被列为空缺:
Time l1 l2 l3 l4 l5
l6
0 11:24:26 0.787261 0.943828 1.100903 0.835889 2.524946
2.252113
1 11:24:26.005000 0.787068 0.943638 1.100871 0.835882 2.531180
2.253063
2 11:24:26.010000 0.786951 0.943496 1.100779 0.835909 2.531573
2.253395
3 11:24:26.015000 0.786879 0.943553 1.100877 0.835877 2.533841
2.254906
4 11:24:26.020000 0.786682 0.943536 1.100651 0.835674 2.539893
2.257780
Time timedelta64[ns]
l1 float64
l2 float64
l3 float64
l4 float64
ecg float64
ppg float64
如何以最有效的方式将时间分配给此文件?
答案 0 :(得分:1)
像这样尝试data.set_index(keys="Time", inplace=True)
:
import pandas as pd
from io import StringIO
data = pd.read_csv(StringIO("""
Time l1 l2 l3 l4 l5 l6
0 11:24:26 0.787261 0.943828 1.100903 0.835889 2.524946 2.252113
1 11:24:26.005000 0.787068 0.943638 1.100871 0.835882 2.531180 2.253063
2 11:24:26.010000 0.786951 0.943496 1.100779 0.835909 2.531573 2.253395
3 11:24:26.015000 0.786879 0.943553 1.100877 0.835877 2.533841 2.254906
4 11:24:26.020000 0.786682 0.943536 1.100651 0.835674 2.539893 2.257780"""), sep="\s+")
data.set_index(keys="Time", inplace=True)
print(data)
输出:
l1 l2 l3 l4 l5 l6
Time
11:24:26 0.787261 0.943828 1.100903 0.835889 2.524946 2.252113
11:24:26.005000 0.787068 0.943638 1.100871 0.835882 2.531180 2.253063
11:24:26.010000 0.786951 0.943496 1.100779 0.835909 2.531573 2.253395
11:24:26.015000 0.786879 0.943553 1.100877 0.835877 2.533841 2.254906
11:24:26.020000 0.786682 0.943536 1.100651 0.835674 2.539893 2.257780
答案 1 :(得分:0)
分配值以代替索引a()
:
x