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

Python 从.dat文件中减去连续行,python,pandas,dataframe,data-manipulation,Python,Pandas,Dataframe,Data Manipulation,我希望从.dat文件中前面的行中减去行,然后从结果中生成一个新列。在我的文件中,我希望在第一列time中这样做,我希望找到每个timestep的时间间隔,然后从中创建一个新列。我从stackoverflow社区获得了帮助,并用python编写了一个伪代码。但到目前为止还不起作用: import pandas as pd import numpy as np from sys import argv from pylab import * import csv script, filename

我希望从.dat文件中前面的行中减去行,然后从结果中生成一个新列。在我的文件中,我希望在第一列time中这样做,我希望找到每个timestep的时间间隔,然后从中创建一个新列。我从stackoverflow社区获得了帮助,并用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()
为了您的方便,我也分享了这个文件,非常感谢您的帮助。

我想你在找
df['time_diff']=df['time'].diff()
@MaxU,你又是老板了!谢谢你,我想你在找的是df['time_diff']=df['time'].diff()@MaxU,你又是老板了!谢谢