Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

readme.md

KeyError: 'salary'

Occurs when trying to access a column that does not exist in a DataFrame.

Reproduce

import pandas as pd

data = {
    "name": ["john", "peter"],
    "age": [30, 25]
}

df = pd.DataFrame(data)

print(df["salary"])

Error Message

KeyError: 'salary'

Fix

import pandas as pd

data = {
    "name": ["john", "peter"],
    "age": [30, 25]
}

df = pd.DataFrame(data)

# Access an existing column
print(df["age"])

Reflection

Assumed the column existed, but it wasn’t in the DataFrame. Spent time checking the data source before verifying the actual column names.