我想使用Python从csv文件打印经度和纬度数据

2024-06-13 08:45:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用Python从csv文件中提取经度和纬度。 文件中有很多行。有经度和纬度行,它们是緯度 和経度. 请原谅一些日本人。 我认为下面的代码能够从文件中提取经度和纬度数据。 当然,实际数字是用经度和纬度列写的。数据应该不是问题

这是我的密码

import pandas as pd

df = pd.read_csv("202.csv", header=None, encoding="shift_jis")

toilet = df[["緯度","経度"]].values
print(len(toilet))
print(toilet)

错误消息

(base) MacBook-Pro:Python_Web_Scraping  macbook_user$ python3 chap5.py
Traceback (most recent call last):
  File "chap5.py", line 6, in <module>
    toilet = df[["緯度","経度"]].values
  File "/Users/macbook_user/opt/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py", line 2908, in __getitem__
    indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
  File "/Users/macbook_user/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py", line 1254, in _get_listlike_indexer
    self._validate_read_indexer(keyarr, indexer, axis, raise_missing=raise_missing)
  File "/Users/macbook_user/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py", line 1298, in _validate_read_indexer
    raise KeyError(f"None of [{key}] are in the [{axis_name}]")
KeyError: "None of [Index(['緯度', '経度'], dtype='object')] are in the [columns]"
(base) MacBook-Pro:Python_Web_Scraping  macbook_user$ 

Tags: csvinpypandasdflinefileraise
1条回答
网友
1楼 · 发布于 2024-06-13 08:45:41

您可能有一个默认列名01,因为您在pd.read_csv中指定了header=None

请尝试以下操作:

  1. 删除header=None
  2. 使用pd.read_csv(.., names=[..])指定列名称

相关问题 更多 >