从python打印excel文件中的数字格式

从python打印excel文件中的数字格式,python,excel,pandas,Python,Excel,Pandas,我有多列负数,整数。其中一些必须采用特定的格式 例如:输入数据帧: Value1 Value2 Value2 1 -5 -50.5 2 35.6 -70 -4 3 -20 预期产量为 Value1 Value2 Value2 1 (5) (50.5) 2 35.6 (70) -4 3 (20) 正如您所看到的,只有Value2和Value3列具有显示在括号中的负值 现在,我尝试了以下方法来显示所需的格式及其工作细节: formatter = lambda x: '(%s)' % str(x)

我有多列负数,整数。其中一些必须采用特定的格式

例如:输入数据帧:

Value1 Value2 Value2
1 -5 -50.5
2 35.6 -70
-4 3 -20
预期产量为

Value1 Value2 Value2
1 (5) (50.5)
2 35.6 (70)
-4 3 (20)
正如您所看到的,只有Value2和Value3列具有显示在括号中的负值

现在,我尝试了以下方法来显示所需的格式及其工作细节:

formatter = lambda x: '(%s)' % str(x)[1:] if x < 0 else str(x)
pd.options.display.float_format = formatter
df6.head()
我需要一种在excel输出文件中正确打印此内容的方法使用:

df6.to_excel('output.xlsx', index=False)
print('Spreadsheet saved.')
df6.applymap(formatter).to_excel('output.xlsx', index=False)