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?
#1
Here's some code:
markets = {'ES':2, 'GC':1, 'CL':2, 'EC':4, 'US':4}
merge_list = [] 
pd.options.display.float_format = '{:.2f}'.format

for market in markets:
    columnname_0 = market+'_Close'
    columnname_1 = market+'_Daily_Rtn'
    columnname_2 = market+'_Cum_Rtn'
    columnname_3 = market+'_Value'
    df_name = market
    market = pd.read_csv(r'C:\Users\drkle\{}(daily).csv'.format(market), parse_dates=["Date"], index_col="Date")
    merge_list.append(market_cut)

merged_df = pd.concat(merge_list, axis=1)
I've since realized I don't want two decimal places for all columns. I changed Line 1 from a list to dictionary with values equal to the desired number of decimal places (in column 0 for each component dataframe). I found this line on SO:
market_cut[df_name] = market_cut[df_name].map('{:,.2f}'.format)
How might I build that out to call the values of markets to format accordingly (e.g. 'ES_Close' column to two decimal places, 'GC_Close' column to one decimal place, 'EC_Close' to four decimal places, etc.)? Alternatively, can you think of another way to do this? Thanks!
Reply
#2
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 on df.style.format. 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) 
Does anyone know what might be wrong with that? No Traceback... just nothing happened.
Reply
#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


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