How to split a panda dataframe into multiple dataframes using a 3X3 window?

Viewed 146

I have a following panda dataframe. I need a code to split this dataframe into multiple dataframes using a 3X3 window.

import pandas as pd

d= {1: [5, 5, 5, 5, 4, 6,11,11], 3: [4, 5, 15, 25, 40, 10,25,33],6: [77, 47, 55, 59, 32,  5,9,22],
    7: [15, 21, 40, 33, 30, 6,14,14],9: [10, 11, 12, 45, 14, 15,13,76],15: [3, 5, 32, 12, 11, 12,17,45],18: [3, 6, 23, 22, 21, 32,19,65],23: [6, 3, 5, 8, 10, 12,4,2]} 

data_miss=pd.DataFrame(data=d)
data_miss.index=[1,3,4,8,10,25,32,40]

See the following picture as a schematic example: enter image description here

As you can see from the above image, the rows and columns indexes are not in sequential numbers and I need to save both rows and column indexes with each extracted dataframe (see the extracted dataframes below the image). Also, the code should be able to consider the borders of the dataframe that the size of selected dataframes in row or col is smaller than 3 and the window should consider it and does not face an error.

1 Answers

you can use twice np.array_split with a double loop: once to split the rows then to split the columns on the already split by 3 rows.

# get dataframe shape
i,c = data_miss.shape

# get all the 3x3 dataframes
l = [arr33 
     for arr3x in np.array_split(data_miss, c//3+1, axis=0) # 3 rows dataframes
     for arr33 in np.array_split(arr3x, i//3+1, axis=1) # 3x3 dataframes
]

# see for example some element of the list
print(l[0])
#    1   3   6
# 1  5   4  77
# 3  5   5  47
# 4  5  15  55
print(l[2]) # only 2 columns
#    18  23
# 1   3   6
# 3   6   3
# 4  23   5
print(l[-1]) # last 2x2 dataframe
#     18  23
# 32  19   4
# 40  65   2
Related