【发布时间】:2019-04-12 23:52:53
【问题描述】:
我有两个数据集。
第一个,在 market 变量中包含具有以下结构的通用市场趋势:
Date High Close Volume Open Low
第二个,在 moods 变量中包含每天几条在这种结构中带有关联情绪的推文:
body date datetime id sentiment time
所以,我想计算每天有多少“看跌”和“看涨”情绪。它有效,这是我的 cmets 代码:
# Read the datasets
market = pd.read_csv("Datasets/SP500/aggregates.txt")
moods = pd.read_json("Datasets/DatasetStockTwits-Aggregato.json")
# Remove all null sentiments
moods = moods[moods.sentiment != "null"]
# Get a generic subsets of data for computational speed
market_tail = market.tail(100)
# For each day present in market_tail, get the same days twits
moods_tail = moods.loc[moods['date'].isin(market_tail.Date)]
# So now I count for each day how many "Bearish" and "Bullish" twits there are
sentiments_count = pd.crosstab(moods_tail['date'], moods_tail['sentiment'])
print(sentiments_count)
这是结果:
sentiment Bearish Bullish
date
2017-11-03 9 12
2017-11-05 3 6
2017-11-06 20 9
2017-11-07 16 35
所以它工作正常,但我不明白为什么我无法访问 sentiments_count.date 或 sentiments_count['date'] 索引。
事实上,如果我尝试这样的事情:
print(sentiments_count['date'])
我得到:KeyError: 'date'
我错过了什么吗? 谢谢
【问题讨论】: