0

Trying to print some stockprices to Pandas Df, and having a hard time to make the stock name get in the same line as the price.

This is my complete code, what am i missing?

import yfinance as yf
from pandas_datareader import data
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame()
tickers = ['ALZR11']
df['Ticker'] = ['']
for ticker in tickers:
    print(ticker)
    df = df.append(pd.DataFrame({'Ticker': ticker}, index=[0]), ignore_index=True)
    ticker = ticker+".SA"
    data = yf.download(tickers=ticker, period="1d", interval="1d")
    df = df.append(data, ignore_index=True)
    #df.loc[ticker, 'New Column Title'] = ticker
    print(df)

Result:

     Ticker  Open  High  Low        Close    Adj Close  Volume
0             NaN   NaN  NaN          NaN          NaN     NaN
1    ALZR11   NaN   NaN  NaN          NaN          NaN     NaN
2       NaN   0.0   0.0  0.0   116.500000   116.500000     0.0

Expected:

     Ticker  Open  High  Low        Close    Adj Close  Volume
0    ALZR11   0.0   0.0  0.0   116.500000   116.500000     0.0

1 Answer 1

1

Let us try , this will make the ticker become the index

for ticker in tickers:
    print(ticker)
    ticker = ticker+".SA"
    data = yf.download(tickers=ticker, period="1d", interval="1d")
    data.index=[ticker]
    df = df.append(data)
    #df.loc[ticker, 'New Column Title'] = ticker
    print(df)
Sign up to request clarification or add additional context in comments.

2 Comments

TypeError: Index(...) must be called with a collection of some kind, 'ALZR11.SA' was passed
@guialmachado add []~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.