首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >条件iloc语句。“时间序列分析”

条件iloc语句。“时间序列分析”
EN

Stack Overflow用户
提问于 2020-01-29 11:46:31
回答 1查看 58关注 0票数 0

在Python中,我有一个包含多行和多列的数据集。如果一个column=X的值,我对它上面的行和右边的列的值感兴趣,所以加上值x的前一个位置的[1,1]

解决这个问题的最好方法是什么?如果是cell =X,有没有一种动态的方法,我可以获得位置,然后添加X数量的行和列,“因为我对那个单元格的值感兴趣”?

Visual Example of Question

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-29 12:33:12

假设我们的数组定义为:

代码语言:javascript
复制
import numpy as np
arr = np.array([[9, 8, 4, 5, 2],
                [0, 3, 5, 6, 7],
                [6, 6, 2, 0, 6],
                [2, 0, 2, 5, 8],
                [2, 6, 9, 7, 9]])

一种直接的解决方案是循环遍历感兴趣的列。

代码语言:javascript
复制
colIndex = 2
X = 2
rDelta = -1 #row change, up is negative!
cDelta = 1 #col change, right is positive

numberRows = np.shape(arr)[1]
output = []

#This loops through all the values in the specified column and looks for 
#matches, if a match is found it is added to an output list
#Note that we only look at values that would result in valid output
#   for example we will not look at the first row if 
#   we would need to index into the row above for the output
for i in range(max(-rDelta,0),min(numberRows,numberRows-rDelta)):
    if(arr[i,colIndex]==X):
        #replace this with your processing
        output.append(arr[i+rDelta,colIndex+cDelta]) 

运行这段代码后,我得到的输出如下

代码语言:javascript
复制
[6, 0]

它们是第二列中2的上边和右边的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59960225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档