Skip to content
Advertisement

AttributeError when applying map() for formatting

Hoping someone can advise on the AttributeError I’m receiving, as I’m not sure what is wrong with the way my code is written. I’ve seen other posts dealing with “‘DataFrame’ object has no attribute”, but it wasn’t applicable to this scenario. Using Python’s map() function to iterate and apply the same formatting across all rows and specified columns, but the map() seems to be the issue. Is there any alternative approach?

Error message:

  File "Z:ReportPythonScriptsreportingtemplateslyReload.py", line 70, in getlyReloadTemplate
    myData[col] = round(myData[col]/1000,0).astype(int).map("{:,}".format)

  File "C:Program FilesAnaconda3libsite-packagespandascoregeneric.py", line 5575, in __getattr__
    return object.__getattribute__(self, name)

AttributeError: 'DataFrame' object has no attribute 'map'

Original code:

for col in [EPRI,LMTR,LastR]:
           myData[col] = round(myData[col]/1000,0).astype(int).map("{:,}".format) 

Advertisement

Answer

Problem is duplicated columns names. So intead Series (one column) your code return all columns with same name.

Test:

for col in [EPRI,LMTR,LastR]:
    print (myData[col])

Solution is deduplicated columns names or remove duplicated columns names:

myData = myData.loc[:, ~myData.columns.duplicated()]
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement