Error in Python Pandas Keyerror: not in index

i have one csv file to study. I learning pandas and an error appeared to me:

import pandas as pd
tpm = pd.read_csv(“tpmteste.csv”,sep=‘;’)
tpm.Percentual = tpm.Percentual[lambda x: x*100]

I am trying to multiple the Percentual column and the error shows up:

KeyError: ‘[58.95, 64.09, 62.33, 62.11, 61.650000000000006, 63.160000000000004] not in index’

Are you trying to find the percentage of a column…and create a new column called “Percent” and populate that column with a percentage?

something probably like(?):

df1['column1_percent'] = (df1['column1'] /
                      df1['column1].sum()) * 100

It’s hard to tell w/o see the first few lines of the df…but, you might have some extra spaces in columns or you might have an spelling error in a column name.

You could try a print(df.columns.tolist()) which will give you all your column names

1 Like

yeah, im trying to fing the percentage. The code should be:

import pandas as pd
tpm = pd.read_csv(“tpmteste.csv”,sep=‘;’)
tpm[‘percentage’] = tpm.percetuation[lambda x: x/100]

But when I type the last line of code, the error show up:

KeyError: ‘[58.95, 64.09, 62.33, 62.11, 61.650000000000006, 63.160000000000004, …] not in index’

I already tried change the columns names. After you said, and still have the issue

I used the you typed, and it works:
The values were in decimal, (like: 0.69), and I was trying to make to 69%, but the error appeared.

thank you for the help. But im trying to use the lambda function to make it, why its not working?

Maybe try something like this instead(?):

df = df.assign(percentage = lambda x: (x['column_name'] /500 * 100))


df['Percent'] = df['col1'].apply(lambda x: (x / df['col1'].sum()) * 100)

Thank you, its works → (df[‘Percent’] = df[‘col1’'].apply(lambda x: x*100)
Apparenttly i was using the wrong method.

tpm[‘percentage’] = tpm.percetuation[lambda x: x*100] → This doenst work!

When we must to cahnge an intire column value, we must to use ‘.apply’, right?

You’re applying the function to the whole column, or series.

See the docs:

1 Like

Use get Method for Safer Access: Consider using df.get('column_name') for columns, which returns None instead of raising an error if the column doesn’t exist.