将 Python pandas DataFrame 中的数字格式化为以千或百万为单位的货币

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41271673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:39:17  来源:igfitidea点击:

Format numbers in a Python pandas DataFrame as currency in thousands or millions

pythonpandasnumbersformatcurrency

提问by thatMeow

I have a dataframe: pd.DataFrame({"Amount":[19000000, 9873200, 823449242]}), and I need to convert the numbers into currency ($) in millions. i.e. $19.00MM, $9.88MM, and $823.45MM.

我有一个数据框:pd.DataFrame({"Amount":[19000000, 9873200, 823449242]}),我需要将数字转换为数百万的货币($)。即 $19.00MM、$9.88MM 和 $823.45MM。

Does anyone know a quick way to do this?

有谁知道一个快速的方法来做到这一点?

Thanks!

谢谢!

回答by EdChum

I think the following should work:

我认为以下应该有效:

df['($) millions'] = '$' + (df['Amount'].astype(float)/1000000).astype(str) + 'MM'

In [3]:
df['($) millions'] = '$' + (df['Amount'].astype(float)/1000000).astype(str) + 'MM'
df

Out[3]:
      Amount   ($) millions
0   19000000        .0MM
1    9873200      .8732MM
2  823449242  3.449242MM

if needed you can also round:

如果需要,您还可以round

In [5]:
df['($) millions'] = '$' + (df['Amount'].astype(float)/1000000).round(2).astype(str) + 'MM'
df

Out[5]:
      Amount ($) millions
0   19000000      .0MM
1    9873200      .87MM
2  823449242    3.45MM

Another method is to apply a formaton each value using apply:

另一种方法是format使用apply以下方法对每个值应用 a :

In [15]:
df['($) millions'] = (df['Amount']/1000000).apply(lambda x: '${:,.2f}MM'.format(x))
df

Out[15]:
      Amount ($) millions
0   19000000     .00MM
1    9873200      .87MM
2  823449242    3.45MM

However, I expect the first method to scale better for large datasets, although sometimes list comprehensions are faster when it comes to strings

但是,我希望第一种方法可以更好地扩展大型数据集,尽管有时列表理解在字符串方面会更快

Here is the list comprehension method:

这是列表理解方法:

In [17]:
df['($) millions'] = ['${:,.2f}MM'.format(x) for x in df['Amount']/1000000]
df

Out[17]:
      Amount ($) millions
0   19000000     .00MM
1    9873200      .87MM
2  823449242    3.45MM

回答by quapka

This simply divides the values - it does not add the $sign etc. (it's only a matter of changing the lambda function), but Amount is still dtype floatso you can treat it as numbers.

这只是将值分开 - 它不添加$符号等(这只是更改 lambda 函数的问题),但 Amount 仍然是 dtype,float因此您可以将其视为数字。

 In [41]: df = pd.DataFrame({"Amount":[19000000, 9873200, 823449242]})

In [42]: df['MillionsAsFloat'] = df.apply(lambda row: row['Amount'] / 1000000, axis=1
    ...: )

In [43]: df['MillionsAsString'] = df.apply(lambda row: '$' + str(round(row['Amount'] 
    ...: / 1000000,2)) + 'MM', axis=1)

In [44]: df
Out[44]: 
      Amount  MillionsAsFloat MillionsAsString
0   19000000        19.000000          .0MM
1    9873200         9.873200          .87MM
2  823449242       823.449242        3.45MM