You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用global_land_mask给CSV添加Land_Sea列?代码无输出排查

问题:使用global_land_mask识别CSV中地点的海陆属性无输出

我有一个包含地点(纬度Latitude、经度Longitude)等变量的CSV文件,希望借助global_land_mask库识别这些地点是否位于陆地,并将结果(True/False)写入名为'Land_Sea'的新列。

数据示例:

StateIDStation_NameLatitudeLongitude
2013TXABIAbilene32.4106-99.6821
2014TXGYFAlaminos Canyon Block 85726.129-94.898
2015TXALIALICE INTL AIRPORT27.7409-98.0269
2016TXE38ALPINE30.384-103.684
2017TXAMAAMARILLO ARPT (AWOS)35.2194-101.706

尝试的代码:

import pandas as pd
from global_land_mask import globe

texas = pd.read_csv('tx.csv')
# Create a column, title it, and put in the data. Syntax: df.insert(col index, 'col name', 'data')
tx_land_sea = texas.insert(5, 'Land_Sea', globe.is_land(texas['Latitude'], texas['Longitude']))
tx_land_sea

运行后无输出,请问问题出在哪里?


问题原因及解决方案

核心问题

pandas.DataFrame.insert()是原地修改方法——它直接在原DataFrametexas中插入新列,不会返回任何有效对象,所以你赋值的tx_land_sea实际上是None,打印tx_land_sea自然看不到结果。

修正后的代码

直接操作原DataFrame,之后查看原对象或者将结果保存为CSV:

import pandas as pd
from global_land_mask import globe

# 读取CSV
texas = pd.read_csv('tx.csv')
# 原地插入Land_Sea列,无需赋值
texas.insert(5, 'Land_Sea', globe.is_land(texas['Latitude'], texas['Longitude']))

# 查看结果(可选)
print(texas)
# 保存修改后的CSV(可选,根据需求执行)
texas.to_csv('tx_with_land_sea.csv', index=False)

额外说明

  • globe.is_land()的参数顺序是纬度在前,经度在后,你的代码中传入texas['Latitude'], texas['Longitude']是正确的,不需要调整。
  • 如果不需要指定列的位置,也可以用更简洁的方式添加列:
    texas['Land_Sea'] = globe.is_land(texas['Latitude'], texas['Longitude'])
    

内容的提问来源于stack exchange,提问作者Deeone

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 18:33:25