简体   繁体   中英

Unable to add values to a pandas DataFrame

I am trying to find the MACD(Moving Average Convergence Divergence) for a few stocks.I am using Pandas_ta, yfinance and pandas libraries. But When I am trying to add the Macd values to the dataframe I am getting this error:

IndexError: iloc cannot enlarge its target object

My code is :

import pandas as pd 
import pandas_ta as ta
import yfinance as yf
import datetime as dt
import matplotlib.pyplot as plt
start=dt.datetime.today()-dt.timedelta(365)
end=dt.datetime.today()
zscore=pd.DataFrame()
rsi=pd.DataFrame()
tickers=['2060.SR' , '2160.SR', '3002.SR', '4007.SR', '3005.SR', '3004.SR' , '2150.SR']
macd=pd.DataFrame()
for i in tickers:
  df=pd.DataFrame(yf.download(i, start=start, end=end, interval="1mo"))

  df.columns = map(str.lower, df.columns)    
  macd=df.ta.macd()
  

Can someone let me know where my mistake is and how to solve this error. thanks

I am not sure which line gave you this error.

But please note that in the loop you are not adding data, but you are re-writing the data again and again:

for i in tickers:
  df=pd.DataFrame(yf.download(i, start=start, end=end, interval="1mo"))

If you want to append, do the following:

agg_df = pd.DataFrame()
for i in tickers:
  df=pd.DataFrame(yf.download(i, start=start, end=end, interval="1mo"))
  agg_df = agg_df.append(df)

df=df.merge(macd, on="Date")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM