【发布时间】:2016-09-24 00:31:52
【问题描述】:
我有一个 .dat 文件,它在一列中有数千行(例如,列是时间,t),现在我想找到列中行之间的间隔,这意味着减去第二行的值从第一行开始,依此类推..(查找 dt)。然后我希望用这些间隔值创建一个新列,并将其绘制在原始列上。如果在这种情况下,除了 python 之外的任何其他语言有帮助,我也很感谢他们的建议。
我为此编写了一个伪 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"})
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
pd.set_option('display.height', 1000)
pd.set_option('display.max_rows', 1000)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
dataframe.plot(x='#time', y='time_delta', style='r')
print dataframe
show()
更新了我的代码,我还分享了我正在处理的 .dat 文件。 https://www.dropbox.com/s/w4jbxmln9e83355/flash.dat?dl=0
【问题讨论】:
-
熊猫移位功能应该可以解决问题。
标签: python mysql pandas data-manipulation