I wrote a script to update a pandas dataframe by accessing the relevant index using .loc and assigning a desired value to it. This worked fine, but as I put these lines of script into a function which uses the dataframe as an argument, I encountered an Attribute error.
I have passed a dataframe into the function as an argument. The function uses .loc to access an element of the dataframe and modify it. The dataframe however is not able to call the .loc function.
Code lines for function (Please see last 2 lines)
def DeltaCalc(dfComponent,code):
ClimateList=dfComponent.Climate.unique() #Finding unique climates
TypologyList=dfComponent.Typology.unique() #Finding unique typologies
CompCodeList=dfComponent.CompCode.unique() #Finding unique component codes
dfDelta=dfComponent[['Climate','Typology','CompCode','RunCode','MaxOT','MedianOT','MinOT','MaxMRT','MedianMRT','MinMRT','TPI','Comfortable','Cooling_unmet','DDH_Cooling','Heating_unmet','DDH_Heating','DDH','DDHSavings']].copy
dfBaselineList=dfComponent[dfComponent.CompCode==code]
for i in range(len(ClimateList)):
if ClimateList[i]=='Cold': #Cold has a different baseline index
continue
for j in range(len(TypologyList)):
dfBaselineTemp=dfBaselineList[(dfBaselineList.Climate==ClimateList[i])&(dfBaselineList.Typology==TypologyList[j])] #Temporary variable storing Baseline for that Climate and Typology combination
for k in range (len(CompCodeList)):
idx=dfComponent[(dfComponent.Climate==ClimateList[i])&(dfComponent.Typology==TypologyList[j])&(dfComponent.CompCode==CompCodeList[k])].index.values
idx=idx[0] # index.values returns a list. Converting to integer
dfDelta.loc[dfDelta.index==idx,'MaxOT']=dfBaselineTemp['MaxOT'].values-dfComponent.loc[dfComponent.index==idx,'MaxOT'].values
dfDelta.loc[dfDelta.index==idx,'MedianOT']=dfBaselineTemp['MedianOT'].values-dfComponent.loc[dfComponent.index==idx,'MedianOT'].values
Code lines for function call
dfDeltaWall=DeltaCalc(dfWall,0)
dfDelta = ... copyshould bedfDelta = ... copy(). i.e. you wantdfDeltato be the result of calling thecopyfunction, not the function itself.