0

I need to change floats to % values in a loop. The problem is when I try this, in the second value of the loop the original data changes to str instead of a float.

for item in correccion_kpis:
    status_general.loc[status_general[header_tracker_KPI] == item, \
        header_final_kpi] = (status_general[header_final_kpi] * 100).map('{:,.2f}%'.format)
    status_general.loc[status_general[header_tracker_KPI] == item, \
        header_tracker_KPI_Objetivo] = status_general[header_tracker_KPI_Objetivo].map('{:,.2f}%'.format)
    status_general.loc[status_general[header_tracker_KPI] == item, \
        header_final_kpi_dia_anterior] = (status_general[header_final_kpi_dia_anterior] * 100).map('{:,.2f}%'.format)
error: ValueError: Unknown format code 'f' for object of type 'str'
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Are you sure that you have floating points and not just strings with numbers? You can check by looking at the value of `status_general[header_tracker_KPI].dtype` – Valentino Jun 14 '19 at 23:36
  • as of pandas 0.17 you don't need to * by 100 to get a percent (i.e 0.12 will return 12%) you just need to add the format. check : https://stackoverflow.com/a/36175424/9375102 – Umar.H Jun 14 '19 at 23:39

0 Answers0