Python Forum
How to format Excel column with comma? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to format Excel column with comma? (/thread-37464.html)



How to format Excel column with comma? - dee - Jun-13-2022

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