-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
  • 11,147
  • 5
  • 36
  • 50
Nickel
  • 580
  • 2
  • 14

1 Answers1

1

Use DataFrame.iloc with slicing:

print (df.iloc[::3])
   x
0  1
3  6
6  9
jezrael
  • 729,927
  • 78
  • 1,141
  • 1,090
  • 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