Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formatting issues?
#2
This works:
ES_mean = comp_df.iloc[:,0].mean(); print('ES_Cum_%ROI mean is {:.2%}'.format(ES_mean))
Port_mean = comp_df.iloc[:,1].mean(); print('Port_Cum_%ROI mean is {:.2%}'.format(Port_mean))
ES_worst = comp_df.iloc[:,0].min(); Port_worst = comp_df.iloc[:,1].min()
ES_std = comp_df.iloc[:,0].std(); Port_std = comp_df.iloc[:,1].std()
print('ES worst = {:.2%}'.format(ES_worst)); print('Port worst = {:.2%}'.format(Port_worst))
holder = comp_df.agg({'ES_Cum_%ROI':['mean', 'std'], 'Port_Cum_%ROI':['mean','std']}) #computing agg before formatting to %
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)
pd.options.display.float_format = '{:.2%}'.format
print(holder)
Because I couldn't process after formatting, I had to add line 6 and call it in line 12. I also had to format twice: lines 7-9 and 11.

Is there an easier way or is this how it must be done?
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 1,863 Jan-06-2023, 06:57 PM
Last Post: deanhystad
  OpenPyXl formatting issues kpayney1 0 2,950 Nov-26-2021, 01:56 AM
Last Post: kpayney1
  Python random formatting issues Barnettch3 10 7,976 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