【发布时间】:2020-01-12 16:42:22
【问题描述】:
我想使用 np.where() 指定一个新的 df 列 df['avg_gain'],其值来自其他两个 df 列之一,df['avg_gain_0'] 或 df['avg_gain_n'],使用的值由时间循环中的周期数('count' 变量)作为条件如下:
starttime = time.time()
count = 0
def x():
klines = client.get_klines(symbol='BTCUSDT', interval=Client.KLINE_INTERVAL_15MINUTE)
df = pd.DataFrame(klines)
df.columns = ['timestart', 'open', 'high', 'low', 'close', 'timeend']
...
df['change'] = df.close.astype(float).diff()
df['gain'] = np.where(df['change'] > 0, df['change'], 0)
df['avg_gain_0'] = df['gain'].rolling(window=6, center=False).mean()
df['avg_gain_n'] = ((float(df['avg_gain'].iloc[-1]) * 5) + df['gain']) / 6
df['avg_gain'] = np.where(count = 0, df['avg_gain_0'], df['avg_gain_n'])
while True:
try:
x()
count += 1
time.sleep(60.0 - ((time.time() - starttime) % 60.0))
用于df['gain'] 的np.where() 函数运行良好,但我收到了一个
关键字参数后的位置参数
用于df['avg_gain'] 的np.where() 函数出错。
我该如何解决?任何帮助表示赞赏。谢谢。
【问题讨论】:
-
df中是否有count列?np.where()的第一个参数需要类似于数组... -
count=0应该做什么? -
count 变量在数据框之外,从 0 开始,每次运行 time/while 循环时加 1。
标签: python python-3.x pandas numpy