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
?
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.
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.
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).
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.
Wouldn't this work?
df2 = df.iloc[:-2:2]
C D
A B
1 4 7 4
3 6 9 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With