Python 使用Pandas,如何使用float_格式修复.sum().sum()的格式

Python 使用Pandas,如何使用float_格式修复.sum().sum()的格式,python,pandas,sum,format,Python,Pandas,Sum,Format,我将以下csv读取为数据帧: >>> test=pd.read_csv('C:/Temp/test.csv') >>> test a b c 0 1.1 2.2 3.3 1 3.0 4.0 5.0 我想把所有这些数字加起来,显示为货币。我认为最有效的方法是使用.sum().sum()方法 >>> pd.set_option('display.float_format','${:,.2f}'.format)

我将以下csv读取为数据帧:

>>> test=pd.read_csv('C:/Temp/test.csv')
>>> test
     a    b    c
0  1.1  2.2  3.3
1  3.0  4.0  5.0
我想把所有这些数字加起来,显示为货币。我认为最有效的方法是使用
.sum().sum()
方法

>>> pd.set_option('display.float_format','${:,.2f}'.format)
>>> test
      a     b     c
0 $1.10 $2.20 $3.30
1 $3.00 $4.00 $5.00
>>> test.sum()
a   $4.10
b   $6.20
c   $8.30
dtype: float64
>>> test.sum().sum()
18.6
我想要的格式化选项用于数据帧和第一个和;但和的和不使用格式,即使数据类型仍然是float:

>>> test.sum().sum().dtype
dtype('float64')
>>> test.sum().dtype
dtype('float64')

如何解决此问题?

test.sum().sum()的输出是一个标量,因此它不应用格式选项。要格式化此类数据,可以使用标准python
format
函数:

"${:,.2f}".format(df.sum().sum())
test.sum()。