pandas 在系列上重置索引以删除多索引 [英] Pandas reset index on series to remove multiindex
问题描述
当我用计数重新采样一些数据时,我从DataFrame
创建了一个Series
像这样:H2
是DataFrame
:
I created a Series
from a DataFrame
, when I resampled some data with a count
like so: where H2
is a DataFrame
:
H3=H2[['SOLD_PRICE']]
H5=H3.resample('Q',how='count')
H6=pd.rolling_mean(H5,4)
这产生了一个看起来像这样的系列:
This yielded a series that looks like this:
1999-03-31 SOLD_PRICE NaN
1999-06-30 SOLD_PRICE NaN
1999-09-30 SOLD_PRICE NaN
1999-12-31 SOLD_PRICE 3.00
2000-03-31 SOLD_PRICE 3.00
具有类似以下内容的索引:
with an index that looks like:
MultiIndex
[(1999-03-31 00:00:00, u'SOLD_PRICE'), (1999-06-30 00:00:00, u'SOLD_PRICE'), (1999-09-30 00:00:00, u'SOLD_PRICE'), (1999-12-31 00:00:00, u'SOLD_PRICE'),.....
我不希望第二列作为索引.理想情况下,我将使用DataFrame
,其中第1列为日期",第2列为销售"(删除索引的第二级).我不太清楚如何重新配置索引.
I don't want the second column as an index. Ideally I'd have a DataFrame
with column 1 as "Date" and column 2 as "Sales" (dropping the second level of the index). I don't quite see how to reconfigure the index.
推荐答案
只需调用reset_index()
:
In [130]: s
Out[130]:
0 1
1999-03-31 SOLD_PRICE NaN
1999-06-30 SOLD_PRICE NaN
1999-09-30 SOLD_PRICE NaN
1999-12-31 SOLD_PRICE 3
2000-03-31 SOLD_PRICE 3
Name: 2, dtype: float64
In [131]: s.reset_index()
Out[131]:
0 1 2
0 1999-03-31 SOLD_PRICE NaN
1 1999-06-30 SOLD_PRICE NaN
2 1999-09-30 SOLD_PRICE NaN
3 1999-12-31 SOLD_PRICE 3
4 2000-03-31 SOLD_PRICE 3
有很多删除列的方法:
调用两次reset_index()
并指定一列:
In [136]: s.reset_index(0).reset_index(drop=True)
Out[136]:
0 2
0 1999-03-31 NaN
1 1999-06-30 NaN
2 1999-09-30 NaN
3 1999-12-31 3
4 2000-03-31 3
重置索引后删除该列:
In [137]: df = s.reset_index()
In [138]: df
Out[138]:
0 1 2
0 1999-03-31 SOLD_PRICE NaN
1 1999-06-30 SOLD_PRICE NaN
2 1999-09-30 SOLD_PRICE NaN
3 1999-12-31 SOLD_PRICE 3
4 2000-03-31 SOLD_PRICE 3
In [139]: del df[1]
In [140]: df
Out[140]:
0 2
0 1999-03-31 NaN
1 1999-06-30 NaN
2 1999-09-30 NaN
3 1999-12-31 3
4 2000-03-31 3
重置后呼叫drop()
:
In [144]: s.reset_index().drop(1, axis=1)
Out[144]:
0 2
0 1999-03-31 NaN
1 1999-06-30 NaN
2 1999-09-30 NaN
3 1999-12-31 3
4 2000-03-31 3
然后,在重置索引后,只需重命名列
Then, after you've reset your index, just rename the columns
In [146]: df.columns = ['Date', 'Sales']
In [147]: df
Out[147]:
Date Sales
0 1999-03-31 NaN
1 1999-06-30 NaN
2 1999-09-30 NaN
3 1999-12-31 3
4 2000-03-31 3
这篇关于 pandas 在系列上重置索引以删除多索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!