Susanne Svensdotter
Susanne Svensdotter

Reputation: 1

KeyError when reading from dataframe

I am creating the dataframe below and I give names to the index lines. I want to read from it using the column and line names.

Creating Dataframe

# propulsion, data from étudeAZEF excel sheet
df_prop = pd.DataFrame({'Propulsion' : ['Helice s PF','Helice xL PF','Helice s PV', 'Helice xL PV','Helice sC/R PV'],'Mass [kg]' : [5, 10, 8, 25, 15],'Efficiency [%]' : [84, 80, 86, 82, 97],'Fixed cost [Euro]' : [2000, 4000, 5000, 6000, 7000],'Life [h]': [10000, 10000, 7000, 3000, 5000]})
df_prop.set_index('Propulsion')
hour_cost = df_prop['Fixed cost [Euro]']/df_prop['Life [h]']
df_prop['Cost per hour [Euro/h]'] = hour_cost

Reading datapoint

# get column index**
ColumnName = "Efficiency [%]"
# get line index**
LineName = "Helice xL PF"
# get datapoint
data = df_prop.loc[LineName,ColumnName]

I get the error message: KeyError: 'Helice xL PF'

If I print the two variables, I get the following :

print(ColumnName)
Efficiency [%]
print(LineName)
Helice xL PF

I have declared both ColumnName and LineName as strings.

Why do I get this error message from trying to read the line? I am following the documentation for df.loc.

I have tried to set LineName as 'Helice xL PF' i.e. with ' instead of " I have tried to give a different name, without spaces :

df_prop = pd.DataFrame({'Propulsion' : ['Helice s PF','Helice2','Helice s PV', 'Helice xL PV','Helice sC/R PV'],
LineName = "Helice2"

I have tried reading a differrent column

LineName = "Helice xL PV"

All of them give the KeyError message.

Upvotes: 0

Views: 32

Answers (0)

Related Questions