有没有办法将插值保存到 python 数据帧?
Is there a way to save interpolated values to a python dataframe?
我正在使用纬度、经度和 z 值获取数据,并使用立方法对其进行插值。这些值似乎保存在 numpy
数组中。有没有办法将插值结果保存到 pandas
数据帧?我试图用经度、纬度和 z 值列保存它作为插值结果。
输入文件 can be found here 并被调用 nutrition.csv。这是我到目前为止所做的:
#Import modules
import pandas as pd
import numpy as np
import os
import shapely
import geopandas as geo
import glob
import holoviews as hv
hv.extension('bokeh')
from scipy.interpolate import griddata, interp2d
import fiona
import gdal
import ogr
#Read file
nut = pd.read_csv('nutrition.csv') #Data to be interpolated
#Minimum and maximum longtude values
lon_min = nut['longitude'].min()
lon_max = nut['longitude'].max()
#Create range of nitrogen values
lon_vec = np.linspace(lon_min, lon_max, 100) #Set grid size
#Find min and max latitude values
lat_min = nut['latitude'].min()
lat_max = nut['latitude'].max()
# Create a range of N values spanning the range from min to max latitude
# Inverse the order of lat_min and lat_max to get the grid correctly
lat_vec = np.linspace(lat_max,lat_min,100,)
# Generate the grid
lon_grid, lat_grid = np.meshgrid(lon_vec,lat_vec)
#Cubic interpolation
points = (nut['longitude'], nut['latitude'])
targets = (lon_grid, lat_grid)
grid_cubic = griddata(points, nut['n_ppm'], targets, method='cubic', fill_value=np.nan)
#Generate the graph
map_bounds=(lon_min,lat_min,lon_max,lat_max)
map_cubic = hv.Image(grid_cubic, bounds=map_bounds).opts(aspect='equal',
colorbar=True,
title='Cubic',
xlabel='Longitude',
ylabel='Latitude',
cmap='Reds')
map_cubic
我认为我真的只需要找到一种方法来组合 targets
地理参考网格(具有插值点的经度和纬度)和 grid_cubic
插值 z-将字段数据放入 pandas 数据帧。
您可以根据获得的数据自行构建数据框
df = pd.DataFrame({
'latitude': lat_grid.reshape(-1),
'longitude': lon_grid.reshape(-1),
'value': grid_cubic.reshape(-1)
});
让我们检查那里的数据,将其转换回 numpy 并绘图
import matplotlib.pyplot as plt
plt.pcolor(np.array(df['longitude']).reshape(100, 100),
np.array(df['latitude']).reshape(100, 100),
np.array(df['value']).reshape(100, 100))
请注意,matplotlib 以不同方式显示轴标签,但坐标与原始数据相同。
如果你想从数据框中删除具有未定义值的行,你可以使用这样的东西
df[df['value'].notna()]
我正在使用纬度、经度和 z 值获取数据,并使用立方法对其进行插值。这些值似乎保存在 numpy
数组中。有没有办法将插值结果保存到 pandas
数据帧?我试图用经度、纬度和 z 值列保存它作为插值结果。
输入文件 can be found here 并被调用 nutrition.csv。这是我到目前为止所做的:
#Import modules
import pandas as pd
import numpy as np
import os
import shapely
import geopandas as geo
import glob
import holoviews as hv
hv.extension('bokeh')
from scipy.interpolate import griddata, interp2d
import fiona
import gdal
import ogr
#Read file
nut = pd.read_csv('nutrition.csv') #Data to be interpolated
#Minimum and maximum longtude values
lon_min = nut['longitude'].min()
lon_max = nut['longitude'].max()
#Create range of nitrogen values
lon_vec = np.linspace(lon_min, lon_max, 100) #Set grid size
#Find min and max latitude values
lat_min = nut['latitude'].min()
lat_max = nut['latitude'].max()
# Create a range of N values spanning the range from min to max latitude
# Inverse the order of lat_min and lat_max to get the grid correctly
lat_vec = np.linspace(lat_max,lat_min,100,)
# Generate the grid
lon_grid, lat_grid = np.meshgrid(lon_vec,lat_vec)
#Cubic interpolation
points = (nut['longitude'], nut['latitude'])
targets = (lon_grid, lat_grid)
grid_cubic = griddata(points, nut['n_ppm'], targets, method='cubic', fill_value=np.nan)
#Generate the graph
map_bounds=(lon_min,lat_min,lon_max,lat_max)
map_cubic = hv.Image(grid_cubic, bounds=map_bounds).opts(aspect='equal',
colorbar=True,
title='Cubic',
xlabel='Longitude',
ylabel='Latitude',
cmap='Reds')
map_cubic
我认为我真的只需要找到一种方法来组合 targets
地理参考网格(具有插值点的经度和纬度)和 grid_cubic
插值 z-将字段数据放入 pandas 数据帧。
您可以根据获得的数据自行构建数据框
df = pd.DataFrame({
'latitude': lat_grid.reshape(-1),
'longitude': lon_grid.reshape(-1),
'value': grid_cubic.reshape(-1)
});
让我们检查那里的数据,将其转换回 numpy 并绘图
import matplotlib.pyplot as plt
plt.pcolor(np.array(df['longitude']).reshape(100, 100),
np.array(df['latitude']).reshape(100, 100),
np.array(df['value']).reshape(100, 100))
请注意,matplotlib 以不同方式显示轴标签,但坐标与原始数据相同。
如果你想从数据框中删除具有未定义值的行,你可以使用这样的东西
df[df['value'].notna()]