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

如何将Pandas DataFrame中lat、lon列转换为指定格式的对象列表

解决方案

你要的转换用Pandas内置的to_dict方法就能一行实现,不需要自己循环遍历拼装数据:

import pandas as pd

# 样例DataFrame构造,可替换为你自己的实际数据对象
df = pd.DataFrame({
    'city': ['London', 'Rome'],
    'lat': [42.33, 92.44],
    'lon': [55.44, 88.11]
})

# 核心转换代码
result = df[['lat', 'lon']].to_dict(orient='records')
print(result)

运行输出就是你需要的格式:

[{'lat': 42.33, 'lon': 55.44}, {'lat': 92.44, 'lon': 88.11}]

说明

  • 先通过df[['lat', 'lon']]筛选出你需要的两列,自动排除city等不需要的字段
  • to_dict方法的orient参数指定为records时,会自动将每一行转换为「列名: 对应值」结构的字典,最终输出字典组成的列表,完全匹配ML Studio的输入要求,不需要额外做格式调整。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 23:06:08