我想找出满足条件RSI < 25
的行。
但是,结果是用一个数据帧生成的。是否可以为任何一行创建单独的数据框?
谢谢。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas_datareader import data as wb
stock='TSLA'
ck_df = wb.DataReader(stock,data_source='yahoo',start='2015-01-01')
rsi_period = 14
chg = ck_df['Close'].diff(1)
gain = chg.mask(chg<0,0)
ck_df['Gain'] = gain
loss = chg.mask(chg>0,0)
ck_df['Loss'] = loss
avg_gain = gain.ewm(com = rsi_period-1,min_periods=rsi_period).mean()
avg_loss = loss.ewm(com = rsi_period-1,min_periods=rsi_period).mean()
ck_df['Avg Gain'] = avg_gain
ck_df['Avg Loss'] = avg_loss
rs = abs(avg_gain/avg_loss)
rsi = 100-(100/(1+rs))
ck_df['RSI'] = rsi
RSIFactor = ck_df['RSI'] <25
ck_df[RSIFactor]
答案 0 :(得分:1)
如果您想知道RSI <25在什么索引上,则只需使用:
ck_df[ck_df['RSI'] <25].index
结果还将是一个数据框。如果您坚持要制作新的,则:
new_df = ck_df[ck_df['RSI'] <25].copy()
答案 1 :(得分:0)
要将@Omkar解决方案找到的行拆分为单独的数据帧,可以使用以下功能:Pandas: split dataframe into multiple dataframes by number of rows;
def split_dataframe_to_chunks(df, n):
df_len = len(df)
count = 0
dfs = []
while True:
if count > df_len-1:
break
start = count
count += n
dfs.append(df.iloc[start : count])
return dfs
由此您将获得数据帧列表。