2017-08-06 16 views
1

次のコードでは、イメージのリストを繰り返し、与えられた数の周波数を数えます。この場合は0と1です。私はこれをcsvに書き出します。私は唯一の周波数のリストを書き出すとき、これは正常に動作しますが、私は、ファイル名を追加しようとすると、私はエラーを取得する:パンダValueError:渡された値の形状

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

私は周波数の一つのリストを書き出すしようとすると(1の数)と、ファイル名はうまく動作します。

次のように私のコードは次のとおりです。

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

パンダあなたは、位置indexcolumns引数としてOneValuesfilesに合格しようとしていると考えています。​​を参照してください。

は辞書であなたのフィールドをラップしてみてください。

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

よろしくお願いします。 –

関連する問題