Broken data frame based on integer index

In pandas, how do I split a series / dataframe into two series / DataFrames where there are odd lines in one series, even lines in different? I'm using now

rng = range(0, n, 2) odd_rows = df.iloc[rng] 

This is pretty slow.

+6
source share
2 answers

Use slice:

 In [11]: s = pd.Series([1,2,3,4]) In [12]: s.iloc[::2] # even Out[12]: 0 1 2 3 dtype: int64 In [13]: s.iloc[1::2] # odd Out[13]: 1 2 3 4 dtype: int64 
+13
source

Here are some comparisons

 In [100]: df = DataFrame(randn(100000,10)) 

simple method (but I think the range makes it slow), but will work regardless of the index (for example, should not be a numeric index)

 In [96]: %timeit df.iloc[range(0,len(df),2)] 10 loops, best of 3: 21.2 ms per loop 

This requires a Int64Index -based Int64Index (which is easy to get, just reset_index() ).

 In [107]: %timeit df.iloc[(df.index % 2).astype(bool)] 100 loops, best of 3: 5.67 ms per loop In [108]: %timeit df.loc[(df.index % 2).astype(bool)] 100 loops, best of 3: 5.48 ms per loop 

do not forget to indicate the position of the index

 In [98]: %timeit df.take(df.index % 2) 100 loops, best of 3: 3.06 ms per loop 

same as above but no negative sign conversions

 In [99]: %timeit df.take(df.index % 2,convert=False) 100 loops, best of 3: 2.44 ms per loop 

This winner is @AndyHayden soln; it only works on one dtype

 In [118]: %timeit DataFrame(df.values[::2],index=df.index[::2]) 10000 loops, best of 3: 63.5 us per loop 
+3
source

Source: https://habr.com/ru/post/948652/


All Articles