Python 排列numpy阵列
我有来自3颗不同卫星的20多张农田卫星图像。每个图像名称都包含数据采集数据和其中的卫星名称。文件名的前两位是月份,后两位是日期,最后一部分包含卫星名称。假设此代码将使用六个图像 每个图像都经过一个循环,在这个循环中它们被处理成numpy数组。代码是-Python 排列numpy阵列,python,numpy,Python,Numpy,我有来自3颗不同卫星的20多张农田卫星图像。每个图像名称都包含数据采集数据和其中的卫星名称。文件名的前两位是月份,后两位是日期,最后一部分包含卫星名称。假设此代码将使用六个图像 每个图像都经过一个循环,在这个循环中它们被处理成numpy数组。代码是- image_list = ["D:/6.10.SkySat.tif", "D:/06.30.SkySat.tif", "D:/06.06.RapidEye.tif", "D:/06.16.RapidEye.tif", "D:/06.26.Plan
image_list = ["D:/6.10.SkySat.tif", "D:/06.30.SkySat.tif", "D:/06.06.RapidEye.tif",
"D:/06.16.RapidEye.tif", "D:/06.26.PlanetScope.tif", "D:/06.30.PlanetScope.tif"]
for image in image_list:
#converting raster image to numpy array
array = arcpy.RasterToNumPyArray(image, nodata_to_value=9999)
#masking out the no data value and converting into one dimentional array
marray = numpy.ma.masked_values(array,9999)
new_array = marray.flatten()
#extracting the date and satellite name
date = image[3:8]
satellite = image[9:-4]
这里我得到一个一维数组、一个日期和一个字符串(卫星名称)。为了进一步使用,我希望它们的格式如下所示。数据将有三列。其中一个包含阵列中的所有像素值,下一个包含日期,最后一个包含卫星名称
Value Date Satellite
0.05825 6/15/2018 SkySat
0.07967976 6/15/2018 SkySat
0.09638854 6/15/2018 SkySat
0.12477265 6/15/2018 SkySat
0.13941683 6/15/2018 SkySat
0.13072205 6/15/2018 SkySat
0.12254229 6/15/2018 SkySat
0.13378483 6/15/2018 SkySat
0.13875392 6/15/2018 SkySat
0.14010076 6/10/2018 PlanetScope
0.1371166 6/10/2018 PlanetScope
0.13878246 6/10/2018 PlanetScope
0.1351179 6/10/2018 PlanetScope
0.16816537 6/10/2018 PlanetScope
0.16348109 6/10/2018 PlanetScope
0.15997969 6/10/2018 PlanetScope
0.16568226 6/10/2018 PlanetScope
0.190534599 6/12/2018 RapidEye
0.219114789 6/12/2018 RapidEye
0.251982007 6/12/2018 RapidEye
0.289779308 6/12/2018 RapidEye
0.333246204 6/12/2018 RapidEye
有没有办法将数据以这种格式排列,然后将其写入CSV或文本文件?欢迎使用Stackoverflow Saurav 我认为您的问题是,您只需要为相应的1-d数组“value”重复“date”和“satellite name”的值 考虑以下示例:
value1 = [1,2,3]
date1 = '1 sep'
satellite_name1 = 'sauravyan'
您可以使用numpy的“重复”功能:
date1 = np.repeat(date1,len(value1))
satellite_name1 = np.repeat(satellite_name1, len(value_1))
使日期数组重复任意次数。案例中的值数组长度
要最终将所有内容转换为csv,我认为最好的方法是
(i) 把每件事都推到一个词汇库:
d['values'].extend(value_1)
d['dates'].extend(date_1)
d['satellites'].extend(s_1)
*记住,在“for”循环之前,用“values”、“dates”和“satellites”作为键创建词汇表
(ii)将词汇表“d”转换为数据帧:
data = pd.DataFrame(d)
(iii)并最终将数据帧转换为csv:
data.to_csv(<filepath/filename.csv>)
把这三个变量都推到字典里
for循环结束后,将词汇表转换为数据帧,然后转换为csv
如有任何疑问,请发表评论
希望有帮助。创建一个
pandas.DataFrame
,pandas带有列=['Value','Date','Satellite']
,并通过将当前数据帧与图像中的新数据帧连接起来,为每个图像在数据帧中附加新数据。
对于在每张图像上生成的数据帧,需要重复日期和卫星信息。您还可以使用pd.to\u datetime
将日期转换为熊猫日期格式。它应该是这样的:
import pandas as pd
import numpy
image_list = ["D:/6.10.SkySat.tif", "D:/06.30.SkySat.tif", "D:/06.06.RapidEye.tif",
"D:/06.16.RapidEye.tif", "D:/06.26.PlanetScope.tif", "D:/06.30.PlanetScope.tif"]
df = pd.DataFrame(columns=['Value', 'Date', 'Satellite'])
for image in image_list:
#converting raster image to numpy array
array = arcpy.RasterToNumPyArray(image, nodata_to_value=9999)
#masking out the no data value and converting into one dimentional array
marray = numpy.ma.masked_values(array,9999)
new_array = marray.flatten()
#extracting the date and satellite name
date = pd.datetime(image[3:8], ignore_errors=True)
satellite = image[9:-4]
df2 = pd.DataFrame({'Value': new_array, 'Date':[date]*new_array.size, 'Satellite':[satellite]*new_array.size})
df = pd.concat([df,df2], ignore_index=True)
print(df) # Should output your expected columns
import pandas as pd
import numpy
image_list = ["D:/6.10.SkySat.tif", "D:/06.30.SkySat.tif", "D:/06.06.RapidEye.tif",
"D:/06.16.RapidEye.tif", "D:/06.26.PlanetScope.tif", "D:/06.30.PlanetScope.tif"]
df = pd.DataFrame(columns=['Value', 'Date', 'Satellite'])
for image in image_list:
#converting raster image to numpy array
array = arcpy.RasterToNumPyArray(image, nodata_to_value=9999)
#masking out the no data value and converting into one dimentional array
marray = numpy.ma.masked_values(array,9999)
new_array = marray.flatten()
#extracting the date and satellite name
date = pd.datetime(image[3:8], ignore_errors=True)
satellite = image[9:-4]
df2 = pd.DataFrame({'Value': new_array, 'Date':[date]*new_array.size, 'Satellite':[satellite]*new_array.size})
df = pd.concat([df,df2], ignore_index=True)
print(df) # Should output your expected columns