You can use slicing
with this formula like that you want [1::2]
([start:end:step])
, here start from 1 and step is 2, So : 1,3,5,....
. Because you have dataframe
, you can use df.index[1::2]
. So you can keep 1, drop 2, keep 3, drop 4, ....
(index start from zero and if you want to start from zero you can try with [::2]
)
import pandas as pd
import numpy as np
df = pd.DataFrame({
'A' : np.random.randint(0,10,1000),
'B' : np.random.randint(0,10,1000)
})
print(df)
df = df.iloc[df.index[1::2]]
print(df)
#input random df
A B
0 1 8
1 5 4
2 8 4
3 9 0
4 9 5
.. .. ..
995 8 9
996 4 9
997 8 4
998 2 8
999 9 0
[1000 rows x 2 columns]
# result random df
A B
1 5 4
3 9 0
5 6 8
7 4 1
9 6 6
.. .. ..
991 1 5
993 6 8
995 8 9
997 8 4
999 9 0
[500 rows x 2 columns]