从.dat文件中减去连续的行

时间:2016-09-25 17:31:09

标签: python pandas dataframe data-manipulation

我希望从.dat文件中的前面的行中减去行,然后从结果中创建一个新列。在我的文件中,我希望在第一列时间内执行此操作,我希望找到每个时间步的时间间隔,然后从中创建一个新列。我从stackoverflow社区获得了帮助,并在pandas python中编写了一个伪代码。但它到目前为止还不起作用:

import pandas as pd
import numpy as np
from sys import argv
from pylab import *
import csv

script, filename = argv

# read flash.dat to a list of lists
datContent = [i.strip().split() for i in open("./flash.dat").readlines()]

# write it as a new CSV file
with open("./flash.dat", "wb") as f:
    writer = csv.writer(f)
    writer.writerows(datContent)

columns_to_keep = ['#time']
dataframe = pd.read_csv("./flash.csv", usecols=columns_to_keep)

df = pd.DataFrame({"#time": pd.date_range("24 sept 2016"),periods=5*24,freq="1h")})
df["time"] = df["#time"]  + [pd.Timedelta(minutes=m) for m in np.random.choice(a=range(60), size=df.shape[0])]
df["value"] = np.random.normal(size=df.shape[0])

df["prev_time"] = [np.nan] + df.iloc[:-1]["time"].tolist()
df["time_delta"] = df.time - df.prev_time
df

dataframe.plot(x='#time', y='time_delta', style='r')
print dataframe
show()

我也是为了您的方便而共享文件,我们非常感谢您的帮助。 https://www.dropbox.com/s/w4jbxmln9e83355/flash.dat?dl=0

0 个答案:

没有答案