如何使用列名在pandas中格式化列

2024-03-29 12:45:19 发布

您现在位置:Python中文网/ 问答频道 /正文

当我把df发送到excel时,我想格式化数据框中的一列,使之在大的数字之间有','。我有一个代码,但它选择的基础上,它的立场列。我需要一个代码来根据列的名称而不是位置来选择列。有人能帮我吗?你知道吗

df.to_excel(writer, sheet_name = 'Final Trade List')

wb = writer.book
ws = writer.sheets['Final Trade List']

format = wb.add_format({'num_format': '#,##'})

ws.set_column('O:O', 12, format) # this code works but its based on position and not name

ws.set_column(df['$ to buy'], 12, format) # this gives me an error

writer.save()

TypeError: cannot convert the series to <class 'int'>


Tags: to代码nameformatdfwscolumnthis