How to break the dataframes by the threshold value?

Viewed 47

I have to break the records by the threshold value and print them one by one. I explain you by example-

I have dataframe with 100 records. My threshold value id 20 records at a time. So I want to print all the records in 5 times breaking 20 records at a time. I tried one sample code but that is not working.

my code:

    j = 0
    for i in (20,df_3.count(), 20):
        print(df_3.iloc[j:i ,:].to_json(orient='records',lines=True))
        time.sleep(5)
        print('----------------------')
        j = i

Can you please help how to get my requirement. This code is not working for me.

2 Answers

You can use only i variable with length of DataFrame and added next 20 rows:

for i in range(0,len(df_3), 20):
     a = df_3.iloc[i:i+20].to_json(orient='records',lines=True)
     print (a)
     time.sleep(5)

If need omit first 20:

for i in range(20,len(df_3), 20):
     a = df_3.iloc[i:i+20].to_json(orient='records',lines=True)
     print (a)
     time.sleep(5)

Slightly different solution:

import pandas as pd 

df = pd.DataFrame({
    'a': range(1, 100),
    'b': range(101, 200)
})

step = 20

for i in range(0, len(df), step):
  with pd.option_context('display.max_rows', None, 'display.max_columns', None):
    print(df[i:i+step])
  print('-----------------------------------------------')
Related