Key error in a column of a csv file with pandas

49 Views Asked by At

I was making a code in python to put the data in a column of a csv file with pandas but when I ran it it gave me a KeyError. The code is this:

import pandas as pd
df = pd.read_csv('archivos\\hi.csv')
name = df['Nombre']
print(name)

The csv file is called ''hi.csv'' and this is its content:

'Nombre','Apellido','Edad'
'Daniela','Alvarez',21
'Eugenia','Ramos',44
'Marcelo','Hernandez',67

As you can see there are no blank spaces in my csv file...

The error I get is this:

Traceback (most recent call last):
  File "c:\Users\PC\OneDrive\Escritorio\Python\archivos\leer.py", line 3, in <module>
    name = df['Nombre']
           ~~^^^^^^^^^^
  File "C:\Users\PC\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\frame.py", line 3893, in __getitem__
    indexer = self.columns.get_loc(key)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\PC\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\indexes\base.py", line 3798, in get_loc
    raise KeyError(key) from err
KeyError: 'Nombre'
PS C:\Users\PC\OneDrive\Escritorio\Python> 

Could someone be so kind as to help me? I'd be really grateful.

When I put

print(df) I get the content.

And if I put

print(df.columns) I get this:


 Index([''Nombre'', ''Apellido'', ''Edad''], dtype='object')

so I assume that the way I wrote the column in the code is fine. I have tried the code on two different computers and the same thing happens. I want the content of the 'Name' column to appear

0

There are 0 best solutions below