1

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:\Report\PythonScripts\reporting\templates\lyReload.py", line 70, in getlyReloadTemplate
    myData[col] = round(myData[col]/1000,0).astype(int).map("{:,}".format)

  File "C:\Program Files\Anaconda3\lib\site-packages\pandas\core\generic.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) 
2020db9
  • 153
  • 1
  • 9
  • Hi @2020db9, can you provide code to generate a minimal example data that reproduces your error? Also, can you try cutting down your code example to isolate the error? (i.e. if you reduce it to just `myData[EPRI].map("{:,}".format)` does it still raise the same error? – ASGM May 10 '22 at 12:24
  • 1
    `.map()` only works for Pandas Series. You need to use `.applymap()` for Pandas Dataframe – Abhyuday Vaish May 10 '22 at 12:30
  • Hi @ASGM , I tried isolating the error (trying one column at a time) and still ran into the same error message. – 2020db9 May 10 '22 at 12:30
  • In that case, you can rewrite your question to take out the `for` loop. And you can try that with all the parts of your original code until you just have the minimum piece that still creates the error. – ASGM May 10 '22 at 12:31

1 Answers1

1

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()]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • You just closed a question using a *harmful* duplicate. You can't parse localized numeric literals using separator replacements – Panagiotis Kanavos May 10 '22 at 12:33
  • 1
    @jezrael it appears you are correct, regarding the duplicated columns, and I'm not sure why they're being duplicated to begin with. However, I did apply your snippet prior to the .map() and it resolved it. – 2020db9 May 10 '22 at 12:37