我怎样才能提取一个部分的Pandas像在下面的图片中标记的框架?

7cwmlq89  于 2023-11-15  发布在  其他
关注(0)|答案(2)|浏览(84)

x1c 0d1x的数据
我试图提取部分(矩阵)的数字在Pandas点阵一样,在上面嵌入的给定图片标记。
请任何人谁可以帮助我,我想执行分析的基础上部分(矩阵)的一个更大的数据框架.提前谢谢你!!

y0u0uwnf

y0u0uwnf1#

您可以使用.iloc[]函数来选择所需的行和列。

dataframe.iloc[5:15,6:15]

字符串
这应该选择第5-14行和第6-14列。不确定数字是否正确,但我认为这种方法是你要找的。
edit:将.loc[]更改为.iloc[],因为我们使用了索引值,并对其进行了一些清理
下面是代码来覆盖整个框架

#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


这基本上和@dafmedinama说的一样,我只是添加了更多的注解和简化了矩阵的形状,如果你不想在每次移动矩阵时都忽略每个数字,我还添加了一个步长变量。

ar5n3qh5

ar5n3qh52#

Be sub_rows和sub_rows是要提取的内存的尺寸:

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

字符串

相关问题