Home > Mobile >  Space de-limited results, errors when Concatenating 2 columns
Space de-limited results, errors when Concatenating 2 columns

Time:11-27

the code I'm running gives results that are space de-liminated. This creates a problem with my sector column which gives a result of Communication Services. It creates 1 column for Communication and another column for Services where I need 1 column saying Communication Services. I have tried to concatentate the 2 columns into 1 but I'm getting attribute and str errors and don't know how to achieve this. Can anyone show how this can be done? Thanks

Code

import yfinance as yf
import pandas as pd
from concurrent.futures import ThreadPoolExecutor

list_of_futures= []

def get_stats(ticker):
    info = yf.Tickers(ticker).tickers[ticker].info
    s= f"{ticker} {info['currentPrice']} {info['marketCap']} {info['sector']}"
    
    list_of_futures.append(s)

ticker_list = ['AAPL', 'ORCL', 'GTBIF', 'META']

with ThreadPoolExecutor() as executor:
    executor.map(get_stats, ticker_list)
    
(
    pd.DataFrame(list_of_futures)
        [0].str.split(expand=True)
        .rename(columns={0: "Ticker", 1: "Price", 2: "Market Cap", 3: "Sector", 4: "Sector1"})
        .to_excel("yahoo_futures.xlsx", index=False)
)

Current Results

enter image description here

Desired Results

enter image description here

CodePudding user response:

Let us reformulate the get_stats function to return dictionary instead string. This way you can avoid the unnecessary step to split the strings to create a dataframe

def get_stats(ticker):
    info = yf.Tickers(ticker).tickers[ticker].info
    cols = ['currentPrice', 'marketCap', 'sector']
    return {'ticker': ticker, **{c: info[c] for c in cols}}

tickers = ['AAPL', 'ORCL', 'GTBIF', 'META']

with ThreadPoolExecutor() as executor:
    result_iter = executor.map(get_stats, tickers)

df = pd.DataFrame(result_iter)

Result

  ticker  currentPrice      marketCap                  sector
0   AAPL        148.11  2356148699136              Technology
1   ORCL         82.72   223027183616              Technology
2  GTBIF         13.25     3190864896              Healthcare
3   META        111.41   295409188864  Communication Services
  • Related