简体   繁体   中英

I want to print longitude and latitude data from a csv file using Python

I want to extract longitude and latitude from a csv file with Python. There are a lot of rows in the file. There are longitude and latitude rows, which are 緯度 and 経度. Pardon some Japanese. I thought the code below would be able to extract longitude and latitude data from the file. Of course, actual figures are written in longitude and latitude columns. The data should not be a problem.

Here is my code

import pandas as pd

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

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

Error message

(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$ 

You probably have a default columns names 0 and 1 because you specified header=None in pd.read_csv .

Try the following:

  1. Remove header=None .
  2. Specify columns names using pd.read_csv(.., names=[..])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM