pd.concat with multilevel columns dataframe

  Kiến thức lập trình

Im newbie in python so Im struggling to make python bot to trading in stock market of my country .

import requests
import time
import json
import pandas as pd
import schedule

Tickers = ['ETHUSDT', 'SOLUSDT']

sub_columns = ['s', 't', 'o', 'h', 'l', 'c', 'v']

multi_index_columns = pd.MultiIndex.from_product([Tickers, sub_columns], names=['Ticker', 'metrics'])
df = pd.DataFrame(columns=multi_index_columns)

def fetch_data():
    global df
    current = int(time.time())
    for Ticker in Tickers:
        url = f"https://Example.com/market/history?symbol={Ticker}&interv=1&countback=1&to={current}"
        response = requests.get(url)
        B = response.json()
        new_data = pd.DataFrame(B)
        df[Ticker] = pd.concat([df[Ticker], new_data], ignore_index=True)
        print(df[Ticker])

So in these part of my code.

First i create a dataframe out of the fetch_data function to save the results of function in it between execusion of fetch_data function. Because of i list two tickers to watch or trading them so i make dataframe in multilevel column (ohlc and volume data for each ticker)

Then in fetch_data function i use

Requests.get(url) and i want this function do it for each of tickers and import the data to relevance top level and sun level columm of main dataframe.

*I wish in the result of every execution of loop it print data in df with correct values but it wasn’t

*when i test the api for each tickers manually its correct but when i test it in tickers loop in show Nan for some values.

Can anyone help me its confusing me for days.

New contributor

RandomYousername is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT