【问题标题】:How to find index for time, lat, lon for particular variable point in xarray如何为xarray中的特定变量点查找时间、纬度、经度的索引
【发布时间】:2021-10-26 06:42:57
【问题描述】:

我打开了以下 netcdf 文件作为 xarray 数据集,其中包含月降水量值。以下是数据集 (ds3) 的样子:

我想隔离高于某个阈值的值并返回每个值的索引。例如:

outliers = ds3.where(ds3.tp > 0.08, drop=True)

for x in outliers.tp:
    print(x)

当我遍历异常值时,它会为我提供每个“tp”值的信息,但我需要相关的索引。 例如,取一个 tp 值为 0.08361223(在上图中),我想返回 time_index(1981-03-01 的索引)、lat_index(8.25 的索引)和 lon_index(38.25 的索引)。 我是 netcdf 文件和 python 的新手,希望得到任何指导。

【问题讨论】:

    标签: multidimensional-array indexing netcdf python-xarray


    【解决方案1】:

    你可以这样写:

    ds3['tp'].where(ds3['tp'] > 0.08, drop=True).to_dataframe().dropna().reset_index()
    

    它会给出一个 pandas DataFrame,其中包含您想要的值及其相关坐标。为了关联整数索引,您可以编写:

    df = ds3['tp'].where(ds3['tp'] > 0.08, drop=True).to_dataframe().dropna().reset_index()
    for c in ds3.indexes:
        df[c] = df[c].apply(lambda v: list(ds3[c].values).index(v))
    

    它不是很优雅,但很有效。

    【讨论】:

    • 这正是我所需要的。到了转换为数据帧的地步,但需要 reset_index() 和关联。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 2021-04-13
    • 2011-10-13
    相关资源
    最近更新 更多