Reputation: 20342
I am testing this code.
# Define the ticker list
from pandas import DataFrame
import pandas as pd
tickers_list = ['AAPL', 'WMT', 'IBM', 'MU', 'BA', 'AXP']
# Fetch the data
import yfinance as yf
company_names = []
for item in tickers_list:
my_stock = yf.Ticker(item)
company_name = my_stock.info['longName']
company_names.append(item + '-' + company_name)
df_names = DataFrame(company_names)
print(df_names)
pd.set_option('display.max_columns', None)
data_hist = yf.download(tickers_list,'2020-05-15')['Adj Close']
print(data_hist.head(5))
The first DF looks like this.
0
0 AAPL-Apple Inc.
1 WMT-Walmart Inc.
2 IBM-International Business Machines Corporation
3 MU-Micron Technology, Inc.
4 BA-The Boeing Company
5 AXP-American Express Company
The second DF looks like this.
AAPL AXP BA IBM MU WMT
Date
5/15/2020 307.709991 82.220001 120 116.980003 44.41 125.940002
5/18/2020 314.959991 88.410004 135.440002 121.559998 45.919998 127.660004
5/19/2020 313.140015 87.260002 130.440002 120.290001 45.119999 124.949997
5/20/2020 319.230011 90.730003 133.320007 121.379997 46.639999 125.449997
5/21/2020 316.850006 89.830002 139 119.120003 45.220001 124.989998
I want to combine these dataframes so the final result looks like this.
Apple Inc. American Express Company The Boeing Company International Business Machines Corporation Micron Technology, Inc. Walmart Inc.
AAPL AXP BA IBM MU WMT
Date
5/15/2020 307.709991 82.220001 120 116.980003 44.41 125.940002
5/18/2020 314.959991 88.410004 135.440002 121.559998 45.919998 127.660004
5/19/2020 313.140015 87.260002 130.440002 120.290001 45.119999 124.949997
5/20/2020 319.230011 90.730003 133.320007 121.379997 46.639999 125.449997
5/21/2020 316.850006 89.830002 139 119.120003 45.220001 124.989998
I think it's going to require some kind of find because I can't match these tickers based on the ordinal position, because the 'yf.download'
seems to alpha-order the names of the fields as it runs. I tried concat
and merge
. Neither worked. Any idea how I can get this working?
Upvotes: 1
Views: 39
Reputation: 2424
Instead of a dataframe, you can use a dictionary:
d = {}
for item in tickers_list:
my_stock = yf.Ticker(item)
company_name = my_stock.info['longName']
d[item] = company_name
print(d)
{'AAPL': 'Apple Inc.',
'WMT': 'Walmart Inc.',
'IBM': 'International Business Machines Corporation',
'MU': 'Micron Technology, Inc.',
'BA': 'The Boeing Company',
'AXP': 'American Express Company'}
And then you update the columns in the second dataframe with map
:
df2.columns = df2.columns.map(d)+" - "+df2.columns
Output:
Apple Inc. - AAPL American Express Company - AXP The Boeing Company - BA International Business Machines Corporation - IBM Micron Technology, Inc. - MU Walmart Inc. - WMT
Date
5/15/2020 307.709991 82.220001 120.000000 116.980003 44.410000 125.940002
5/18/2020 314.959991 88.410004 135.440002 121.559998 45.919998 127.660004
5/19/2020 313.140015 87.260002 130.440002 120.290001 45.119999 124.949997
5/20/2020 319.230011 90.730003 133.320007 121.379997 46.639999 125.449997
5/21/2020 316.850006 89.830002 139.000000 119.120003 45.220001 124.989998
Upvotes: 1
Reputation: 3382
Simply create a dictionary {ticker : ticker+name}
and than convert the column names:
d={'AAPL':'Apple Inc. AAPL'} #etc, simply split df1.str values by "-" to create this dict
df2.columns = [d.get(c) for c in df2.columns]
Upvotes: 0