Pandas DataFrame slicing with iteration

核能气质少年 提交于 2019-12-01 06:33:43

问题


I would like to perform some action on sliced DataFrame with multiple slice indexes. The pattern is df.iloc[0:24] , df.iloc[24:48], df.iloc[48:72] and so on with step 24 as you get it. How I can iterate it without to set it manually every time. More like df.iloc[x:z] and each iteration x=0, z=24 and next iteration with 24 step, x will be 24 and z=48 and so on. Thanks in advance, Hristo.


回答1:


for loop iteration

for i in range(0, len(df), 24):
    slc = df.iloc[i : i + 24]

groupby

df.groupby(df.index // 24 * 24).apply(your_function)



回答2:


output it will give df with

for i in range(0,len(df)):
  dfnew = df.iloc[i : i + 42]
  i = i + 1
  print(dfnew)

0 to 41 indices data 1 to 42 2 to 43 by skipping first entire row with limit 42 rows of data.



来源:https://stackoverflow.com/questions/47337328/pandas-dataframe-slicing-with-iteration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!