Python Forum
How to format Excel column with comma?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to format Excel column with comma?
#1
I want to format the number columns with commas. I received an error "column.map = '{:,d}'.format AttributeError: 'str' object has no attribute 'map'". How can I format the columns? Thanks.

df.to_excel(writer, sheet_name='Sheet Name', index=False)

# Auto-adjust columns width
for column in df:
    column_width = max(df[column].astype(str).map(len).max(), len(column))
    col_idx = df.columns.get_loc(column)
    writer.sheets['Sheet Name'].set_column(col_idx, col_idx, column_width)
    if (column == 'Amount1') or (column == 'Amount2') or (column == 'Amount3'):
        column.map = '{:,d}'.format
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,874 Dec-12-2022, 08:22 PM
Last Post: jh67
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,114 Aug-19-2022, 08:07 PM
Last Post: Winfried
  Converting cells in excel to JSON format desmondtay 4 1,739 May-23-2022, 10:31 AM
Last Post: Larz60+
  decimal comma DPaul 9 2,268 Feb-22-2022, 12:25 PM
Last Post: DeaD_EyE
  Appending Excel column value as CSV file name sh1704 0 1,298 Feb-06-2022, 10:32 PM
Last Post: sh1704
  Need Help writing data into Excel format ajitnayak87 8 2,503 Feb-04-2022, 03:00 AM
Last Post: Jeff_t
  Exporting dataframes to excel without loosing datetime format Rafa 0 1,235 Oct-27-2021, 10:42 AM
Last Post: Rafa
  Want to remove the text from a particular column in excel shantanu97 2 2,139 Jul-05-2021, 05:42 PM
Last Post: eddywinch82
  Adding a comma in the resulting value stsxbel 6 2,639 May-22-2021, 09:24 PM
Last Post: stsxbel
  Copy column from one existing excel file to another file mkujawsk 0 5,613 Apr-14-2021, 06:33 PM
Last Post: mkujawsk

Forum Jump:

User Panel Messages

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