Pandas Keep every other row group
That is to say, I have a pandas dataframe like this:
0 1 2
0 1 2 3
1 4 5 6
2 7 8 9
3 10 11 12
4 13 14 15
5 16 17 18
And I would like to keep any other group of 2 lines, i.e. the end result looks like this:
0 1 2
0 1 2 3
1 4 5 6
4 13 14 15
5 16 17 18
I know how to get interleaved lines using df.iloc [:: 2], but that gives me:
0 1 2
0 1 2 3
2 7 8 9
4 13 14 15
Be healthy if someone can point me in the right direction here, not sure if it can be done with iloc but if someone can point me in the right direction that would be much appreciated
+3
source to share
2 answers
There are many ways to do this - it should be noted that in repeating pattern 4, you need the first two, or:
In [18]: df.loc[np.arange(len(df)) % 4 < 2]
Out[18]:
0 1 2
0 1 2 3
1 4 5 6
4 13 14 15
5 16 17 18
because
In [19]: np.arange(len(df))
Out[19]: array([0, 1, 2, 3, 4, 5])
In [20]: np.arange(len(df)) % 4
Out[20]: array([0, 1, 2, 3, 0, 1])
In [21]: np.arange(len(df)) % 4 < 2
Out[21]: array([ True, True, False, False, True, True], dtype=bool)
+6
source to share