Python Forum
Can I format decimal places by column with a dictionary?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can I format decimal places by column with a dictionary?
#3
This works:
format_mapping = {markets[0]+'_Close':'{:.2f}',markets[1]+'_Close':'{:.1f}',markets[2]+'_Close':'{:.2f}', markets[3]+'_Close':'{:.4f}', markets[4]+'_Close':'{:.4f}'}
for key, value in format_mapping.items():
    merged_df[key] = merged_df[key].apply(value.format)
I did a lot of reading about df.style.format . I don't understand why this did not work:
format_dict = {markets[0]+'_Close':'{:.2f}',markets[1]+'_Close':'{:.1f}',markets[2]+'_Close':'{:.2f}', markets[3]+'_Close':'{:.4f}', markets[4]+'_Close':'{:.4f}'}
merged_df.style.format(format_dict) #WHY DID THIS NOT WORK?
I didn't get a Traceback. I just didn't see any formatting occur. Does it have to do with printing a df rather than a table?
Did I have to do something else to print out the Styler object (which did seem to be there when I typed merged_df.style and/or merged_df.style.format)?

I'm using Python 3.7.9.
Reply


Messages In This Thread
RE: Can I format decimal places by column with a dictionary? - by Mark17 - Dec-28-2020, 10:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Calculated DF column from dictionary value plantagenet 2 867 Sep-16-2022, 12:46 AM
Last Post: plantagenet
  How to format Excel column with comma? dee 0 1,366 Jun-13-2022, 10:11 PM
Last Post: dee
  Calculate the Euler Number with more decimal places Pedroski55 10 4,504 Oct-31-2021, 04:45 AM
Last Post: Pedroski55
  format the output from a nested dictionary. nostradamus64 9 4,555 May-03-2021, 04:45 PM
Last Post: nostradamus64
  Not rounding to desired decimal places? pprod 2 2,560 Mar-05-2021, 11:11 AM
Last Post: pprod
  JupyterLab Dictionary Content Output Format Ourkid123uk 0 1,314 Sep-04-2020, 02:18 PM
Last Post: Ourkid123uk
  Issue accessing data from Dictionary/List in the right format LuisSatch 2 2,211 Jul-25-2020, 06:12 AM
Last Post: LuisSatch
  Using OpenPyXL How To Read Entire Column Into Dictionary jo15765 1 2,684 Jun-08-2020, 04:10 AM
Last Post: buran
  testing for Decimal w/o importing decimal every time Skaperen 7 4,452 May-06-2019, 10:23 PM
Last Post: Skaperen
  str.format rounding to the left of the decimal ClassicalSoul 2 2,486 Mar-27-2019, 11:12 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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