Home > OS >  how to find values above threshold in pandas and store them with date
how to find values above threshold in pandas and store them with date

Time:03-30

I have a DF with stock prices and I want to find stock prices for each day that are above a threshold and record the date, percent increase and stock name.

import pandas as pd
import requests
import time
import pandas as pd
import yfinance as yf

stock_ticker=['AAPL','MSFT','LCID','HOOD','TSLA]

df = yf.download(stock_tickers, 
                      start='2020-01-01', 
                      end='2021-06-12', 
                      progress=True,
)

data=df['Adj Close']
data=data.pct_change()
data.dropna(inplace=True)

top=[]
for i in range(len(data)):
  if i>.01 :
   top.append(data.columns[i])

I tried to do a for loop but it saves all the tickers name What I want to do is find the stocks for each day that increased by 1% and save the name, date and percent increase in a pandas.

Any help would be appreciate it

CodePudding user response:

There might be a more efficient way, but I'd use DataFrame.iteritems(). An example attached below. I kept duplicated Date index since I was not sure how you'd like to keep the data.

data = df["Adj Close"].pct_change()
threshold = 0.01

df_above_th_list = []
for item in data.iteritems():
    stock = item[0]
    sr_above_th = item[1][item[1] > threshold]
    df_above_th_list.append(pd.DataFrame({"stock": stock, "pct": sr_above_th}))
df_above_th = pd.concat(df_above_th_list)

If you want to process the data by row, you can use DataFrame.iterrows() or DataFrame.itertuples().

  • Related