Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split odd rows of DataFrame without double iloc

Test df:

import pandas as pd

df = pd.DataFrame({'A':[1,2,3,7,8,4],
                   'B':[4,5,6,1,4,6],
                   'C':[7,8,9,2,7,3],
                   'D':[4,5,2,1,0,6]})

df.set_index(['A','B'], inplace=True)
print (df)
     C  D
A B      
1 4  7  4
2 5  8  5
3 6  9  2
7 1  2  1
8 4  7  0
4 6  3  6

Splitted by odd rows:

print (df.iloc[::2])
     C  D
A B      
1 4  7  4
3 6  9  2
8 4  7  0

I need split it again - last row in df1 and all other rows to df2:

df1 = df.iloc[-2::2]
df2 = df.iloc[::2].iloc[:-1,:]

print (df1)
     C  D
A B      
8 4  7  0

print (df2)
     C  D
A B      
1 4  7  4
3 6  9  2

Is better solution without double iloc?

like image 217
jezrael Avatar asked Jun 20 '16 07:06

jezrael


People also ask

How do I delete rows in Pandas DataFrame based on multiple conditions?

Pandas provide data analysts a way to delete and filter data frame using dataframe. drop() method. We can use this method to drop such rows that do not satisfy the given conditions.

How do you slice rows in Pandas?

Slicing Rows and Columns by Index PositionWhen slicing by index position in Pandas, the start index is included in the output, but the stop index is one step beyond the row you want to select. So the slice return row 0 and row 1, but does not return row 2. The second slice [:] indicates that all columns are required.

What is the difference between the use of ILOC and Loc?

The main distinction between loc and iloc is: loc is label-based, which means that you have to specify rows and columns based on their row and column labels. iloc is integer position-based, so you have to specify rows and columns by their integer position values (0-based integer position).

How do I select rows in ILOC?

You have to pass parameters for both row and column inside the . iloc and loc indexers to select rows and columns simultaneously. The rows and column values may be scalar values, lists, slice objects or boolean.


1 Answers

Wouldn't this work?

df2 = df.iloc[:-2:2]

     C  D
A B      
1 4  7  4
3 6  9  2
like image 58
PdevG Avatar answered Oct 02 '22 02:10

PdevG