如何重新索引通过折叠数据帧创建的序列?

2024-10-03 09:07:28 发布

您现在位置:Python中文网/ 问答频道 /正文

考虑以下代码:

sfix = sub['fix']  # a pandas.Panel
(sfix.minor_xs('tstop') - sfix.minor_xs('tstart'))  # slicey slicey!

输出:

^{pr2}$

此输出对应于Panel对象中包含的8个数据帧的tstop和{}列之间的差异。在

这些列都包含相同类型的数据,我想将它们堆叠成一个系列,因此:

s = pd.concat([df[i] for i in df])

这是一个好的开始,但现在我的所有索引都被复制了8次:

>>> s.ix[0]

0     98
0    184
0    178
0    188
0    176
0    234
0    128
0     82
dtype: float64

从这里开始,我不太清楚如何重新索引我的系列,使索引从0变为len(s)。我试过以下几种方法,但没有效果:

s.reindex(copy=True)
s.reindex(index=xrange(len(s)), copy=True)

我错过了什么?在


Tags: 数据代码truepandasdflenfixcopy
2条回答

IIUC,您可以使用reset_index(drop=True)

>>> s
0     98
0    184
0    178
0    188
0    176
0    234
0    128
0     82
Dtype: float64
>>> s.reset_index(drop=True)
0     98
1    184
2    178
3    188
4    176
5    234
6    128
7     82
Dtype: float64

这也应该行得通

s = pd.concat([df[i] for i in df], ignore_index = True)

相关问题 更多 >