我想用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$
您可能有一个默认列名
0
和1
,因为您在pd.read_csv
中指定了header=None
请尝试以下操作:
header=None
李>pd.read_csv(.., names=[..])
指定列名称相关问题 更多 >
编程相关推荐