【发布时间】:2019-10-23 17:29:10
【问题描述】:
我正在尝试按行业过滤股票代码符号。我找不到使用我创建的字典来引入所有股票代码的方法。如何遍历字典中的键以在其各自列表中引入股票代码?我对python比较陌生,我相信有一个相对简单的方法,我就是找不到。
我的数据框如下所示:
Symbol industry
TXG Biotechnology
YI Medical
PIH Property Insurers
PIHPP Property Insurers
除了还有数千行。
# 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)
我收到错误代码:AttributeError: 'dict' object has no attribute 'append'
我期待一个看起来像这样的字典:
industries = {Biotechnology: ['TXG']
Medical: ['YI']
Property Insurers: ['PIH', 'PIHPP']}
我知道您可以在数据框中手动输入每个行业以单独过滤每个列表,但由于有数千行数据,我正在寻找像我上面这样的迭代,只是一个有效的迭代。
谢谢!
【问题讨论】:
-
这是字典还是熊猫数据框?你引用两个
-
industries[key].append...
标签: python pandas dataframe filter iteration