删除Pandas中的第n行

Yol*_*ken 3 python datetime resampling pandas

我有一个Pandas df,时间序列是34毫秒,我只需要5秒的分辨率.我最初创建了一个时间戳,并尝试将时间戳设置为索引并重新取样和.iloc.

# Defining file path
file = "C:/file/path/data.csv"

# Read in data and parse date/time to DateTime format
data = pd.read_csv(file,header=10,parse_dates=[[0,1]],dayfirst=False)

# time stamp in preferred format
data['date_stamp'] = pd.to_datetime(data['Date_ Time'],dayfirst=False)

#trying to get every 5 seconds, not 34 milliseconds
data.iloc[::15,:]

# saving new file to csv
data.to_csv(""C:/file/path/data.csv"",date_format='%Y%m%d %H:%M:%S')
Run Code Online (Sandbox Code Playgroud)

这是最好的做时间索引和重新取样吗?此代码始终返回df中的相同数据.什么是将这些数据压缩成5秒间隔的最佳方法?

jez*_*ael 5

我想你可以用resample用first:

data.set_index('date_stamp', inplace=True)
print (data.resample('5S').first())
Run Code Online (Sandbox Code Playgroud)

查看文档

如果使用年龄较大的熊猫0.18.0:

print (data.resample('5S', how='first'))
Run Code Online (Sandbox Code Playgroud)