Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formatting issues?
#1
Here's some code:
format_mapping2 = {'ES_Cum_%ROI':'{:.2%}','Port_Cum_%ROI':'{:.2%}','Port_Marg_Tot':'{:.2f}'}
for key, value in format_mapping2.items():
    comp_df[key] = comp_df[key].apply(value.format)
print(comp_df)
comp_df = comp_df.apply(pd.to_numeric)
print(comp_df.dtypes)
ES_mean = comp_df.iloc[:,0].mean()
With lines 1-3, comp_df prints with the formatting I want.

I then want to do further processing, though... like get mean, std, min/max, etc. When I include the last line, I get: ValueError: Unable to parse string "276.50%" at position 0 . If I take out line 5, then I get TypeError: Could not convert 272.31%165.15% to numeric (that's why I tried WITH line 5).

So--when I format these columns as percentages, am I actually changing them to strings (with or without lines 1-3, dtypes show up as "object" for all columns)? And if they are strings, how to I change them back to numeric so I can proceed with the further processing? Thanks!
Reply


Messages In This Thread
Formatting issues? - by Mark17 - Dec-30-2020, 04:03 PM
RE: Formatting issues? - by Mark17 - Dec-30-2020, 04:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  String formatting (strptime) issues Henrio 2 843 Jan-06-2023, 06:57 PM
Last Post: deanhystad
  OpenPyXl formatting issues kpayney1 0 1,685 Nov-26-2021, 01:56 AM
Last Post: kpayney1
  Python random formatting issues Barnettch3 10 5,966 Jan-28-2018, 11:04 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020