Pandas every nth row

前端 未结 5 1220
别那么骄傲
别那么骄傲 2020-11-30 19:40

Dataframe.resample() works only with timeseries data. I cannot find a way of getting every nth row from non-timeseries data. What is the best method?

5条回答
  •  孤城傲影
    2020-11-30 20:02

    Though @chrisb's accepted answer does answer the question, I would like to add to it the following.

    A simple method I use to get the nth data or drop the nth row is the following:

    df1 = df[df.index % 3 != 0]  # Excludes every 3rd row starting from 0
    df2 = df[df.index % 3 == 0]  # Selects every 3rd raw starting from 0
    

    This arithmetic based sampling has the ability to enable even more complex row-selections.

    This assumes, of course, that you have an index column of ordered, consecutive, integers starting at 0.

提交回复
热议问题