从条件中读取和返回数组中的值

时间:2016-04-25 07:55:41

标签: python python-2.7 pandas

我正在尝试从我创建的数组中读取并从其找到的列和行返回数组内的值。这就是我现在所拥有的。

import pandas as pd
import os
import re

Dir = os.getcwd()
Blks = []

for files in Dir:
    for f in os.listdir(Dir):
        if re.search('txt', f):
            Blks = [each for each in os.listdir(Dir) if each.endswith('.txt')]
print (Blks)

for z in Blks:
    df = pd.read_csv(z, sep=r'\s+', names=['x','y','z'])
    a = []    
    a = df.pivot('y','x','z')
    print (a)

输出:

x       300.00  300.25  300.50  300.75  301.00  301.25  301.50  301.75  
y                                                                        
200.00     100     100     100     100     100     100     100     100   
200.25     100     100     100     100     110     100     100     100   
200.50     100     100     100     100     100     100     100     100

x将是我的列,y是行,数组内部是与相邻列和行对应的值。正如你在上面看到的那样,有一个奇数110的值比其他值高10,我试图读取数组并返回值的x(列)和y(行)值。通过检查其旁边的值(顶部,底部,右侧,左侧)来计算差异。

希望有人可以引导我走向正确的方向,任何初学者的提示都会受到赞赏。如果不清楚我在问什么,请问我没有多年的所有方法经验,我最近才开始用python。

1 个答案:

答案 0 :(得分:0)

You could use DataFrame.ix to loop through all the values, row by row and column by column.

oddCoordinates=[]

for r in df.shape[0]:
   for c in df.shape[1]:
      if checkDiffFromNeighbors(df,r,c):
         oddCoordinates.append((r,c))

The row and column of values that are different from the neighbors are listed in oddCoordinates.

To check the difference between the neighbors, you could loop them and count how many different values there are:

def checkDiffFromNeighbors(df,r,c):
   #counter of how many different
   diffCnt = 0
   #loop over the neighbor rows
   for dr in [-1,0,1]:
      r1 = r+dr
      #row should be a valid number
      if r1>=0 and r1<df.shape[0]:
         #loop over columns in row
         for dc in [-1,0,1]:
            #either row or column delta should be 0, because we do not allow diagonal
            if dr==0 or dc==0:
               c1 = c+dc
               #check legal column
               if c1>=0 and c1<df.shape[1]:
                  if df.ix[r,c]!=df.ix[r1,c1]:
                     diffCnt += 1

   # if diffCnt==1 then a neighbor is (probably) the odd one
   # otherwise this one is the odd one 
   # Note that you could be more strict and require all neighbors to be different
   if diffCnt>1:
      return True
   else:
      return False