简体   繁体   English

如何在 python 的一个数据集中通过多个经度和纬度组合数据 arrays

[英]How to combine data arrays by multi longitude and latitude in one dataset in python

I made for loop of my data array of each long/lat point and I appended all result in one list as below:我对每个 long/lat 点的数据数组进行了 for 循环,并将所有结果附加到一个列表中,如下所示:

out_list=[]
for i in ds.longitude.values:
    for j in ds.latitude.values:
        point = arr.sel(longitude=i,latitude=j)
        
        p_detrend = sm.tsa.tsatools.detrend(point, order=1,axis=0)
        out_list.append(p_detrend)

and my list as below:我的清单如下: 在此处输入图像描述 and you can see there are many arrays and each one has long/lat.你可以看到有很多 arrays 并且每个都有长/纬度。 How to combine all arrays in one dataset by longitude and latitude?如何按经度和纬度将所有 arrays 组合在一个数据集中?

maybe you could use dictionary instead, something like:也许您可以改用字典,例如:

out_list = []

for i in ds.longitude.values:
    for j in ds.latitude.values:
        point = arr.sel(longitude=i,latitude=j)
        p_detrend = sm.tsa.tsatools.detrend(point, order=1,axis=0)

        result = {
            'longitude': i, 
            'latitude': j,
            'detrend_arr': p_detrend  
        }
        out_list.append(result)

then you may use pandas to convert into pd.DataFrame for other manipulation;然后您可以使用 pandas 转换为pd.DataFrame进行其他操作;

What if you use xarray.merge() ?如果你使用xarray.merge()怎么办?

I used the example from documentation and your description to create a fake data for different longitudes and latitudes, all stacked in a list.我使用 文档中的示例和您的描述为不同的经度和纬度创建了一个假数据,所有这些数据都堆叠在一个列表中。

Then, if I use xarray.merge() , it will automatically structure the data into ('time','longitude','latitude') xarray object.然后,如果我使用xarray.merge() ,它会自动将数据构造成('time','longitude','latitude') xarray object。

import numpy as np
import pandas as pd
import xarray as xr

np.random.seed(123)

xr.set_options(display_style="html")

times = pd.date_range("2000-01-01", "2001-12-31", name="time")
annual_cycle = np.sin(2 * np.pi * (times.dayofyear.values / 365.25 - 0.28))

ds = []

for lon in range(-40,-30):
    for lat in range(0,10):
        base = 10 + 15 * annual_cycle.reshape(-1,1,1)
        tmin = base + 3 * np.random.randn(annual_cycle.size,1,1)
        tmax = base + 10 + 3 * np.random.randn(annual_cycle.size,1,1)

        ds.append(xr.Dataset(
            {
                "tmin": (("time", "latitude","longitude"), tmin),
                "tmax": (("time", "latitude","longitude"), tmax),
            },
            {"time": times,"latitude":[lat],"longitude":[lon]},
        ))
ds = xr.merge(ds)

First, ds will be like a list of different xarray objects for each latitude and longitude:首先, ds就像每个纬度和经度的不同xarray对象的列表:

[<xarray.Dataset>
 Dimensions:    (latitude: 1, longitude: 1, time: 731)
 Coordinates:
   * time       (time) datetime64[ns] 2000-01-01 2000-01-02 ... 2001-12-31
   * latitude   (latitude) int64 0
   * longitude  (longitude) int64 -40
 Data variables:
     tmin       (time, latitude, longitude) float64 -4.646 -3.527 ... -3.939
     tmax       (time, latitude, longitude) float64 8.436 1.992 ... 3.366 1.605,
 <xarray.Dataset>
 Dimensions:    (latitude: 1, longitude: 1, time: 731)
 Coordinates:
   * time       (time) datetime64[ns] 2000-01-01 2000-01-02 ... 2001-12-31
   * latitude   (latitude) int64 1
   * longitude  (longitude) int64 -40
 Data variables:
     tmin       (time, latitude, longitude) float64 -2.966 -3.322 ... -7.731
     tmax       (time, latitude, longitude) float64 3.156 5.467 ... 11.85 7.26,
 <xarray.Dataset>
.
.
.
.
]

Then, after the merge, it will be a xarray.dataset just like this:然后,在合并之后,它将是一个xarray.dataset ,就像这样:

代表

Please, let me know if that helps, or if I need to adapt in some way to work for your specific type of data.请让我知道这是否有帮助,或者我是否需要以某种方式适应您的特定类型的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 训练 model 时数据集中的纬度和经度数据 - Latitude and Longitude data in dataset while training the model 如何在geodash2(Python)中将纬度和经度编码为一个值? - How to encode latitude and longitude in one value in geodash2 (Python)? 如何使用 python 获取纬度和经度 - How To Get Latitude & Longitude with python 使用Python按经度和纬度对CSV数据进行排序 - Sorting csv data by longitude and latitude with Python 如何使用 python 中的纬度和经度数据绘制地图并突出显示地图中的几个纬度和经度点? ints - How can i plot a map using Latitude and longitude data in python & Highlight few Latitude and Longitude points in the map ?ints 如何快速将纬度和经度放在一列? - How put latitude and longitude in one column quickly? 如何将HEXEWKB转换为Latitude,Longitude(在python中)? - How to convert HEXEWKB to Latitude, Longitude (in python)? 如何在python中将经度转换为十进制? - How to convert latitude longitude to decimal in python? 如何在 xarray 数据集中使用纬度和经度变量作为维度/坐标? - How to use latitude and longitude variables in a xarray dataset as dimensions/coordinates? 使用 python 进行经纬度聚类 - Clustering with longitude and latitude with python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM