无法在特定时间从数据框熊猫获取值

问题描述 投票:0回答:1

我具有以下功能:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import csv
from datetime import datetime, timedelta
def AvgPrice (file,min1):
    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(file, header=None, names=headers, dtype=dtypes, parse_dates=parse_dates)
    now3 = pd.DataFrame(btcnow, columns=['size','time','price'])
    for i in range (1,11) :
        time_interval = timedelta(minutes = min1)
        df = now3.loc[now3['size']==i, ['size','time','price']]

        # extract time size for merge
        df_time_size=df.loc[:, ['time', 'size']].copy()
        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['price_orig']=df.loc[[df['time']- time_interval],'price']
        #gets the last price at the time
        df=df.groupby('time').last().reset_index()
  return df

代码now3数据帧如下:

size                time   price
0           4.0 2019-11-03 02:42:00  9288.5
1           4.0 2019-11-03 02:42:00  9288.5
2           4.0 2019-11-03 02:42:00  9288.5
3           4.0 2019-11-03 02:42:00  9288.5
4           4.0 2019-11-03 02:42:00  9288.5
...         ...                 ...     ...
1048570    15.0 2019-11-05 05:48:00  9331.0
1048571  3851.0 2019-11-05 05:48:00  9331.0
1048572  3793.0 2019-11-05 05:48:00  9331.0
1048573  1000.0 2019-11-05 05:48:00  9331.0
1048574   200.0 2019-11-05 05:48:00  9331.0

我正在尝试在5分钟内获得每种尺寸1-11的时间和价格,以及原始价格(5分钟前)。我在此行出现错误:df['price_orig']=df.loc[[df['time']- time_interval],'price']我不确定5分钟前如何记录价格。

python pandas dataframe time price
1个回答
0
投票
您可以尝试这个,

for row in df.itertuples(): vals = df.loc[df.time == row.orig_time, 'price'].values if len(vals) > 0: df.loc[row.Index, 'orig_price'] = vals[0]

而不是,

df['price_orig'] = df.loc[[df['time'] - time_interval], 'price']

© www.soinside.com 2019 - 2024. All rights reserved.