我想从我的数据中获取原始价格并将其与尺寸匹配(如下所示) 我有以下代码:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import csv
headers = ['ticker', 'size', 'price', 'unix','type','time']
dtypes = {'ticker': 'str', 'size': 'float', 'price': 'float', 'unix': 'float','type': 'str','time': 'str'}
parse_dates = ['time']
btcnow = pd.read_csv('new 113-115.csv', header=None, names=headers, dtype=dtypes, parse_dates=parse_dates)
now3 = pd.DataFrame(btcnow, columns=['size','time','unix','price'])
from datetime import datetime, timedelta
time_interval = timedelta(minutes = 5)
df = now3[[ 'time', 'size', 'price']]
# extract time size for merge
df_time_size= df[['time', 'size']]
df_time_size.loc[:, 'time'] = df_time_size.loc[:, 'time'] + time_interval
# inner join dataframe by size&time
df = df_time_size.merge(df[['time', 'size', 'price']], how = 'inner')
df['orig_time'] = df['time'] - time_interval
df=df.groupby('time').last().reset_index()
df1= df.loc[df["size"] == 4, "price"]
df2= df.loc[df["size"] == 4, "time"]
df3= df.loc[df["size"] == 4, "size"]
df4=df.loc[df["size"] == 4, "orig_time"]
frames = [df3,df1,df2,df4]
result = pd.concat(frames, axis=1, sort=True)
print(result)
它打印出以下(帧):
size price time orig_time
4 4.0 9300 2019-11-03 02:47:00 2019-11-03 02:42:00
116 4.0 9244.5 2019-11-03 04:43:00 2019-11-03 04:38:00
170 4.0 9240.5 2019-11-03 05:37:00 2019-11-03 05:32:00
297 4.0 9176.5 2019-11-03 07:44:00 2019-11-03 07:39:00
351 4.0 9149.5 2019-11-03 08:38:00 2019-11-03 08:33:00
etc.
我也希望能够捕获原始价格并将其添加到数据框中。 从now3数据帧开始:
size time unix price
0 4.0 2019-11-03 02:42:00 1.570000e+12 9288.5
1 4.0 2019-11-03 02:42:00 1.570000e+12 9288.5
2 4.0 2019-11-03 02:42:00 1.570000e+12 9288.5
3 4.0 2019-11-03 02:42:00 1.570000e+12 9288.5
4 4.0 2019-11-03 02:42:00 1.570000e+12 9288.5
例如,上面的代码为我提供了2:47的价格的最后价格,该价格是2:42的5分钟,但是如何将原始价格也捕获到第一张桌子(框架)中?所以我的输出应如下所示:
size price orig_price time orig_time
116 4.0 9300 9288.5 2019-11-03 2:47:00 2019-11-03 2:42:00
170 4.0 9240.5 9500 2019-11-03 05:37:00 2019-11-03 05:32:00
答案 0 :(得分:0)
我知道了。我先做了
now4=now3[['time','price','size']]
df6= now4.loc[now4["size"] == 4,'size']
df7= now4.loc[now4["size"] == 4, "time"]
df8= now4.loc[now4["size"] == 4, "price"]
frames1 = [df6,df7,df8]
result1 = pd.concat(frames1, axis=1, sort=True)
result1.columns = ['size','orig_time','price']
df10=result1.groupby('orig_time').last().reset_index()
df10 = df10[['size','orig_time','price']]
设置类似的数据框,然后将其与上面的数据框合并。