Sep-02-2021, 11:31 AM
In the below script I am creating 2 datasets (stats1, stats2). One in each for loop. Now I want to combine the two data sets. That is, adding the columns from stats2 after the last column in stats1. I don't know how to do that. If somebody could help that would be great. Note: I need to delete "level_1" in each data set.
import pandas as pd
from yahoo_fin import stock_info as si
ti = pd.read_csv('/Users/detlefschmitt/Desktop/TickerList.csv')
stock_list = ti["Ticker"].tolist()
stats1 = {}
stats2 = {}
for ticker in stock_list:
data = si.get_stats(ticker)
stats1[ticker] = data
for ticker in stock_list:
data = si.get_stats_valuation(ticker)
stats2[ticker] = data
combined = pd.concat(stats1)
combined = combined.reset_index()
del combined["level_1"]
combined.columns = ["Ticker", "Attribute", "Recent"]
df = pd.DataFrame(combined)
df2 = df.pivot(index='Ticker', columns='Attribute', values='Recent')
df2.to_csv(r'/Users/detlefschmitt/Desktop/stats.csv')
