Python 熊猫多索引:如何将多索引(YYYY,MM)合并为单个(YYYY-MM)索引?
我已经研究这个问题一段时间了,我真的很接近了。本质上,我想从事件数据库中创建一个按类型划分的事件计数时间序列。我真的很接近。以下是我迄今为止所做的工作: 从my dataframe的缩写版本开始:Python 熊猫多索引:如何将多索引(YYYY,MM)合并为单个(YYYY-MM)索引?,python,pandas,dataframe,Python,Pandas,Dataframe,我已经研究这个问题一段时间了,我真的很接近了。本质上,我想从事件数据库中创建一个按类型划分的事件计数时间序列。我真的很接近。以下是我迄今为止所做的工作: 从my dataframe的缩写版本开始: event_date year time_precision event_type \ 0 2020-10-24 2020 1 Battles 1 2020-10-24 2020 1 Riots 2 2
event_date year time_precision event_type \
0 2020-10-24 2020 1 Battles
1 2020-10-24 2020 1 Riots
2 2020-10-24 2020 1 Riots
3 2020-10-24 2020 1 Battles
4 2020-10-24 2020 2 Battles
year 1997 ... 2020
month 1 2 3 4 5 6 ... 5 6 7
fatalities 11 30 38 112 17 29 ... 1322 1015 619
Battles 4 4 5 13 2 2 ... 77 99 74
Explosions/Remote violence 2 1 0 0 3 0 ... 38 28 17
Protests 1 0 0 1 0 1 ... 31 83 50
Riots 3 3 4 1 4 1 ... 27 14 18
Strategic developments 1 0 0 0 0 0 ... 7 2 7
Violence against civilians 3 5 7 3 2 1 ... 135 112 88
我希望时间序列按月份和年份显示,因此首先我将日期转换为datetime
:
nga_df.event_date = pd.to_datetime(nga_df.event_date)
然后,我想按类型创建事件的时间序列,因此我对它们进行了一次热编码:
nga_df = pd.get_dummies(nga_df, columns=['event_type'], prefix='', prefix_sep='')
接下来,我需要提取月份,以便创建月度计数:
nga_df['month'] = nga_df.event_date.apply(lambda x: x.month)
最后,我离这里很近,我将我的数据按月份和年份分组,并进行换位:
conflict_series = nga_df.groupby(['year','month']).sum()
conflict_series.T
这就产生了这个可爱的新数据帧:
event_date year time_precision event_type \
0 2020-10-24 2020 1 Battles
1 2020-10-24 2020 1 Riots
2 2020-10-24 2020 1 Riots
3 2020-10-24 2020 1 Battles
4 2020-10-24 2020 2 Battles
year 1997 ... 2020
month 1 2 3 4 5 6 ... 5 6 7
fatalities 11 30 38 112 17 29 ... 1322 1015 619
Battles 4 4 5 13 2 2 ... 77 99 74
Explosions/Remote violence 2 1 0 0 3 0 ... 38 28 17
Protests 1 0 0 1 0 1 ... 31 83 50
Riots 3 3 4 1 4 1 ... 27 14 18
Strategic developments 1 0 0 0 0 0 ... 7 2 7
Violence against civilians 3 5 7 3 2 1 ... 135 112 88
所以,我想我需要做的是合并我的索引(转置后的列),使它们成为一个索引。我该怎么做
最终目标是将这些数据与经济指标结合起来,看看是否有趋势,因此我需要两个数据集采用相同的形式,其中列是不同值的月度计数。我是这样做的:
步骤1:展平索引:
# convert the multi-index to a flat set of tuples: (YYYY, MM)
index = conflict_series.index.to_flat_index().to_series()
步骤2:添加任意但需要的月份,以便转换为日期时间:
index = index.apply(lambda x: x + (28,))
步骤3:将生成的三元组转换为日期时间:
index = index.apply(lambda x: datetime.date(*x))
步骤4:重置数据帧索引:
conflict_series.set_index(index, inplace=True)
结果:
fatalities Battles Explosions/Remote violence Protests Riots \
1997-01-28 11 4 2 1 3
1997-02-28 30 4 1 0 3
1997-03-28 38 5 0 0 4
1997-04-28 112 13 0 1 1
1997-05-28 17 2 3 0 4
Strategic developments Violence against civilians total_events
1997-01-28 1 3 14
1997-02-28 0 5 13
1997-03-28 0 7 16
1997-04-28 0 3 18
1997-05-28 0 2 11
现在我正在寻找的情节是:
Doh。找到了。我将留下我的问题,以防它有助于其他人回答。如果你解决了自己的问题,请发布自己的答案并接受。@JohnZwinck正在处理它。