如何重新索引通过合并DataFrame创建的序列?
考虑下面的代码:
sfix = sub['fix'] # a pandas.Panel
(sfix.minor_xs('tstop') - sfix.minor_xs('tstart')) # slicey slicey!
输出结果:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 804 entries, 0 to 803
Data columns (total 8 columns):
0 573 non-null values
1 675 non-null values
2 804 non-null values
3 715 non-null values
4 578 non-null values
5 568 non-null values
6 664 non-null values
7 599 non-null values
dtypes: float64(8)
这个输出结果是每个包含在面板对象中的8个数据框的 tstop
和 tstart
列之间的差值。
这些列里包含的是相同类型的数据,我想把它们堆叠成一个单一的序列,所以:
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)
我漏掉了什么呢?
2 个回答
2
如果我理解得没错,你可以使用 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
1
这个也应该可以用
s = pd.concat([df[i] for i in df], ignore_index = True)