Im folgenden Code iteriere ich eine Liste von Bildern und zähle die Häufigkeiten einer gegebenen Zahl, in diesem Fall Nullen und Einsen. Ich schreibe das dann an einen CSV. Dies funktioniert gut, wenn ich die Liste der Frequenzen auszuschreiben nur, aber wenn ich versuche, die Dateinamen hinzufügen dann bekomme ich den Fehler:Pandas ValueError: Form der übergebenen Werte
ValueError: Shape of passed values is (1, 2), indices imply (2, 2)
Wenn ich versuche, eine Liste von Frequenzen zu schreiben (Anzahl der Einsen) und Die Dateinamen funktionieren einwandfrei.
Mein Code ist wie folgt:
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)
sie in einem dict gearbeitet Verpackung! Danke für den Link zur Dokumentation –
Gut, und gern geschehen! –