#df = big data frame
shape = (10,10) #shape of matrix to be analyzed, here is 10x10
step = 1 #step size, iterate over every number
#or
step = 10 #step size, iterate block by block
#keep in mind, iterating by block will leave some data out at the end of the rows and columns
#you can set step = shape if you are working with a matrix that isn't square, just be sure to change step in the code below to step[0] and step[1] respectively
for row in range( 0, len(df[0]) - shape[0]+1, step): #number of rows of big dataframe - number of rows of matrix to be analyzed
for col in range(0, len(df.iloc[0,:]) - shape[1]+1, step): #number of columns of big dataframe - number of columns of matrix to be analized
matrix = df.iloc[row:shape[0]+row, col:shape[1]+col] #slice out matrix and set it equal to 'matrix'
#analize matrix here
import pandas as pd
sub_rows = 10 # Amount of rows to be extracted
sub_cols = 3 # Amount of columns to be extracted
if sub_rows > len(df.index):
print("Defined sub dataframe rows are more than in the original dataframe")
elif sub_cols > len(df.columns):
print("Defined sub dataframe columns are more than in the original dataframe")
else:
for i in range(0,len(df.index)-sub_rows):
for j in range(0, len(df.columns)):
d.iloc[i:i+sub_rows, j:j+sub_cols] # Extracted dataframe
# Put here the code you need for your analysis
2条答案
按热度按时间y0u0uwnf1#
您可以使用
.iloc[]
函数来选择所需的行和列。字符串
这应该选择第5-14行和第6-14列。不确定数字是否正确,但我认为这种方法是你要找的。
edit:将.loc[]更改为.iloc[],因为我们使用了索引值,并对其进行了一些清理
下面是代码来覆盖整个框架
型
这基本上和@dafmedinama说的一样,我只是添加了更多的注解和简化了矩阵的形状,如果你不想在每次移动矩阵时都忽略每个数字,我还添加了一个步长变量。
ar5n3qh52#
Be sub_rows和sub_rows是要提取的内存的尺寸:
字符串