我有两个数据集。
第一个,在市场变量中,包含一个具有以下结构的通用市场趋势:
Date High Close Volume Open Low第二个问题是,在心情中,变量每天都包含一些在这种结构中带有无表情情绪的tweet:
body date datetime id sentiment time所以,我想每天数一数到底有多少“看跌”和“看涨”情绪。它可以工作,这是我的代码,并附有注释:
# 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'
我是不是错过了什么?谢谢
发布于 2018-11-09 11:54:45
您不能选择它,因为它是index,所以需要:
print(sentiments_count.index)对于从index需要reset_index创建列,也可以为数据清理添加rename_axis以删除列名sentiment
sentiments_count = sentiments_count.reset_index().rename_axis(None, 1)
print(sentiments_count['date'])https://stackoverflow.com/questions/53225248
复制相似问题