简体   繁体   中英

Python Pandas DF append to same row

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

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)

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