2013-03-25 60 views
0

考虑下面的代码:如何重新编制通过折叠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) 

该输出对应于为每个包含在Panel对象的8个DataFrames的tstoptstart列之间的差异。

这些列中都含有相同类型的数据,我想他们堆到一个单一系列,ERGO:

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) 

我错过了什么?

回答

1

这应该工作太

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

谢谢!这似乎正是我想要的。只是为了加倍确定:这是否保留了数据的顺序? – blz 2013-03-25 19:51:31

+0

我相信它(这项工作) - pd.concat([pd.DataFrame({0:[0]}),pd.DataFrame({0:[1]})],ignore_index = True) – user1827356 2013-03-25 20:18:31

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 
相关问题