I want to store the "info" from a list of the SP500 symbols in a Pandas DataFrame.
I can do this one at a time (df1, df2,...) and then append all into one DataFrame (df):
but would like to have a function that automates this by iterating through a list of symbols and appends new rows each time to 'df'.
Any ideas?? Thanks!
CodePudding user response:
Try this:
import pandas as pd
import yfinance as yf
l = ['AAPL', 'MMM']
df_list = []
for t in l:
df_list.append(pd.DataFrame([yf.Ticker(t).info]))
df = pd.concat(df_list)
print(df)
CodePudding user response:
You can load one and then loop through the list of the others:
import yfinance as yf
import pandas as pd
# first stock
data=yf.Ticker('AAPL')
dfall= pd.DataFrame([data.info])
# list of the others
liststock=['MMM','GOOG','MSFT']
# append all
for stock in liststock:
data=yf.Ticker(stock)
df= pd.DataFrame([data.info])
dfall=pd.concat([dfall, df],axis=0)

