-1

I have one dataframe, i want to get first row of each 3 rows in dataframe and save new dataframe

here is input data

df=pd.DataFrame({'x':[1,2,5,6,7,8,9,9,6]})

output:

df_out=pd.DataFrame({'x':[1,6,9]})
Joe
  • 12,057
  • 5
  • 39
  • 55
Nickel
  • 580
  • 4
  • 19

1 Answers1

1

Use DataFrame.iloc with slicing:

print (df.iloc[::3])
   x
0  1
3  6
6  9
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • I don't know why you use `.iloc`? `df[::3]` works too, no? – Corralien Jul 30 '21 at 06:13
  • @Corralien - ya, but not for columns, more genearal is used for slicing by position `iloc`. But agree, for slice by index in df/series is possible omit `iloc` – jezrael Jul 30 '21 at 06:17