asdlfkjlkj
asdlfkjlkj

Reputation: 2348

Split dataframe into multiple df

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

Upvotes: 0

Views: 2169

Answers (3)

niraj
niraj

Reputation: 18208

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

Upvotes: 0

tryingToLearn
tryingToLearn

Reputation: 11659

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

Upvotes: 0

PyRsquared
PyRsquared

Reputation: 7338

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)

Upvotes: 2

Related Questions