0

For example, if I have a dataframe, df like this

col1 col2 col3
 1     2     34
 11    32    32
 21    62    34
 31    12    31
 13    82    35
 11    32    33
 41    32    33

I want to loop 3 times and in each loop I want to take n rows one after another and in the last loop take rest of the rows. So it should take following rows in each loop

loop 1

 1     2     34
 11    32    32

loop 2

21    62    34
 31    12    31

loop 3

13    82    35
 11    32    33
 41    32    33
1
  • This is a basic programming thing, it has nothing to do with pandas. What code you tried to write for this, please let us know Commented Sep 23, 2017 at 12:18

3 Answers 3

2

Use numpy array_split

import numpy as np
num_chunks = 3
np.array_split(df,num_chunks) # this will split your array into num_chunks

You can assign new variables to each chunk as such

chunk1,chunk2,chunk3 = np.array_split(df,num_chunks)
Sign up to request clarification or add additional context in comments.

2 Comments

how can I do it using loop? probably by dividing size of the df?
Why do you care about using a loop?
0

You can use iterrows() method provided by dataframe and you can put some specific condition as per your requirement. For e.g. I could quickly come up with the following code, though it can be improved, but it prints the way you want to print it:

import pandas as pd

data = [[1,  2,  34],
        [11, 32, 32],
        [21, 62, 34],
        [31, 12, 31],
        [13, 82, 35],
        [11, 32, 33],
        [41, 32, 33]]

df = pd.DataFrame(data,columns=['col1','col2','col3'])

n = 2

count = 0
for index, row in df.iterrows():
   if count == n or count == 2*n:
       print("New line")
   print(row['col1'], row['col2'], row['col3'])
   count = count + 1

Comments

0

If you want to use loop, you can try following:

n = 2
loop_var = [(i+n) if (i+n+n)<len(df) else len(df) for i in range(0, len(df)-1, n)]
start = 0
for i in loop_var:
    print(df[start:i])
    start = i

Result:

   col1  col2  col3
0     1     2    34
1    11    32    32
   col1  col2  col3
2    21    62    34
3    31    12    31
   col1  col2  col3
4    13    82    35
5    11    32    33
6    41    32    33

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.