Calculating RSI from a pandas dataframe

1k Views Asked by At

I have a pandas dataframe where each column of the dataframe corresponds to the closing price of a given stock (IBOVESPA-BRASIL). I want to calculate the RSI for each dataframe stock (df0) and create a new dataframe with this data (df1). I am trying to use the pandas-ta library, but I got stuck in the parameter that corresponds to the closing price. How can I fix this?

#!pip install yfinance
#!pip install pandas-ta

#Used Packages
import pandas as pd
import pandas_ta as ta
from pandas_datareader import data as pdr
import yfinance as yf
yf.pdr_override()
from datetime import datetime
import warnings
warnings.filterwarnings("ignore")

#Analysis Start and End Date
inicio = '2020-04-01'
fim = '2021-04-27'

#STOCKS
lista = ['LCAM3.SA','MOVI3.SA','CARD3.SA','PRIO3.SA']

#GET DATA YFINANCE
data = pd.DataFrame()
stocks=lista
for stock in stocks:
  try:
    data[stock] = pdr.DataReader(stock, data_source="yahoo", start=inicio, end=fim)["Close"]
  except:
    continue
df0 = data

#GET RSI FROM DF0
df1 = df0.ta.rsi(close=df0, length=14, scalar=None, drift=None, offset=None)```
1

There are 1 best solutions below

0
On

Don't believe you can do all columns at once. Use list comprehension to iterate over lista and create new dataframe. The T is transpose to get the right orientation.

df1 = pd.DataFrame([df0.ta.rsi(close=df0[sym], length=14, scalar=None, drift=None, offset=None) for sym in lista]).T
df1.columns = lista