0

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)
3
  • dfDelta = ... copy should be dfDelta = ... copy(). i.e. you want dfDelta to be the result of calling the copy function, not the function itself. Commented Jul 4, 2023 at 17:06
  • Error msg: AttributeError Traceback (most recent call last) Cell In[13], line 1 ----> 1 dfDeltaWall=DeltaCalc(dfWall,0) Cell In[12], line 19, in DeltaCalc(dfComponent, code) ---> 19 dfDelta.loc[dfDelta.index==idx,'MaxOT']=dfBaselineTemp['MaxOT'].values-dfComponent.loc[dfComponent.index==idx,'MaxOT'].values AttributeError: 'function' object has no attribute 'loc' Commented Jul 4, 2023 at 17:08
  • Thanks @slothrop Worked like a charm. I just edited to ...copy(deep=True). It is working now. Commented Jul 4, 2023 at 17:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.