I am trying to filter stock ticker symbols by their industry. I can't find a way to use the dictionary I created to bring in all of the ticker symbols. How can I iterate through the keys in my dictionary to bring in the stock symbols in their respective list? I am relatively new to python and I am sure there is a relatively easy way, I just can't find it.
My dataframe looks like this:
Symbol industry
TXG Biotechnology
YI Medical
PIH Property Insurers
PIHPP Property Insurers
except there are thousands more rows.
# I'm bringing in the values from the column 'industry' and create a dictionary:
industries_var = all_tickers['industry'].values
industries = {industry_name: [] for industry_name in industries_var}
# now I want to iterate through the name of every list in my dictionary
# and append the matching symbol to the industry name in the dataframe:
for key in industries:
if all_tickers['industry'].str.contains(key, na=False).any():
industries.append(all_tickers['Symbol'].values)
I am getting the error code: AttributeError: 'dict' object has no attribute 'append'
I am expecting a dictionary looking something like this:
industries = {Biotechnology: ['TXG']
Medical: ['YI']
Property Insurers: ['PIH', 'PIHPP']}
I know you can manually type in every industry in the dataframe to filter every list individually, but because there are thousands of lines of data I am looking for an iteration like mine above, just a working one.
Thank you!