Jun-13-2022, 10:11 PM
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
