michael michael - 6 months ago 32
Python Question

Groupby with row index operation?

How can I select the rows with given row index operation (say, only even rows or only if row# % 5 == 0) in pandas?
let's say I have a dataframe with df

[120 rows x 10 column]
, and I want to create two out of it, one from even rows df1
[60 rows x 10 column]
, and one from odd rows
[60 rows x 10 column]


You can slice the dfs using normal list style slicing semantics:

first = df.iloc[::2]
second = df.iloc[1::2]

So the first steps every 2 rows starting from first to last row the second does the same but starts from row 1, the second row, and steps every 2 rows