Pandas every nth row

前端 未结 5 1218
别那么骄傲
别那么骄傲 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

    I had a similar requirement, but I wanted the n'th item in a particular group. This is how I solved it.

    groups = data.groupby(['group_key'])
    selection = groups['index_col'].apply(lambda x: x % 3 == 0)
    subset = data[selection]
    

提交回复
热议问题