2017-08-06 48 views
1

在下面的代碼中,我遍歷圖像列表並計算給定數字的頻率,在這種情況下爲零和1。然後我寫出來給csv。我只寫了頻率表,但是當我嘗試添加的文件名,然後我得到的錯誤也能正常工作:Pandas ValueError:傳遞值的形狀

ValueError: Shape of passed values is (1, 2), indices imply (2, 2) 

當我嘗試寫出來的頻率中的一個列表(的人的數量)和它的文件名工作正常。

我的代碼如下:

import os 
from osgeo import gdal 
import pandas as pd 
import numpy as np 

# Input directory to the .kea files 
InDir = "inDirectory" 

# Make a list of the files 
files = [file for file in os.listdir(InDir) if file.endswith('.kea')] 

# Create empty list to store the counts 
ZeroValues = [] 
OneValues = [] 

# Iterate through each kea file and open it 
for file in files: 
    print('opening ' + file) 
    # Open file 
    ds = gdal.Open(os.path.join(InDir, file)) 
    # Specify the image band 
    band = ds.GetRasterBand(1) 
    # Read the pixel values as an array 
    arr = band.ReadAsArray() 
    # remove values that are not equal (!=) to 0 (no data) 
    ZeroPixels = arr[arr==0] 
    OnePixels = arr[arr==1] 
    print('Number of 0 pixels = ' + str(len(ZeroPixels))) 
    print('Number of 1 pixels = ' + str(len(OnePixels))) 
    # Count the number of values in the array (length) and add to the list 
    ZeroValues.append(len(ZeroPixels)) 
    OneValues.append(len(OnePixels)) 
    # Close file 
    ds = Non 

# Pandas datagram and out to csv 
out = pd.DataFrame(ZeroValues, OneValues, files) 
# Write the pandas dataframe to a csv 
out.to_csv("out.csv", header=False, index=files) 

回答

2

大熊貓認爲你試圖通過OneValuesfiles作爲位置indexcolumns參數。見​​。

嘗試在一個字典包裹你的領域:

import pandas as pd 

ZeroValues = [2,3,4] 
OneValues = [5,6,7] 
files = ["A.kea","B.kea","C.kea"] 

df = pd.DataFrame(dict(zero_vals=ZeroValues, one_vals=OneValues, fname=files)) 

輸出:

fname one_vals zero_vals 
0 A.kea   5   2 
1 B.kea   6   3 
2 C.kea   7   4 
+0

在字典包裹他們的工作!感謝您連接到文檔 –

+0

好,歡迎您! –

相關問題