Error accessing Dictionary Code

Asked

Viewed 52 times

0

I create a program to read from a file csv a set of coordinates and stores them in an object DataFrame. The code goes below

df = pandas.read_csv(os.getcwd() + "/Coordinates.csv")
print(df["Longitude"])

The csv file is as follows:

Longitude;Latitude
10;10
11.3;11.9
12.4;10.3
9.2;10.4

When running this code the following error is shown:

  File "/home/bruno/.local/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2522, in get_loc
 return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 117, in 
  pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1265, in 
  pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
  KeyError: 'longitude'

When printing column value with obtain:

Index(['Longitude;Latitude'], dtype='object')

My question is whether the columns are being read correctly, why the keyError

1 answer

0


The archive .csvwas generated incorrectly. Using ; as a separator or actually generates a single index Longitude;Latitude.
The right color is to use , as a separator in the input file. This way two indexes will be generated: longitude and latitude

Browser other questions tagged

You are not signed in. Login or sign up in order to post.