我试图将数字XXXXXX.xx转换成货币格式"XXX,XXX.xx $“。
案例1和2,使用熊猫数据:
Amount Currency #1 ($) Currency #2
0 1000000 1,000,000 1,000,000 $
1 97320.25 97,320.25 97,320.25 $
2 9000000.1 9,000,000.10 9,000,000.10 $就代码的简单性而言,哪种方法是完成此任务的最佳方法?
df['Currency #1'] = df['Amount'].ToString("###,###,###.00") ??
df['($) Currency #2'] = df['Amount'].apply(lambda x...??
df['($) Currency #2'] = df['Amount'].SomeInternalFunction() ??发布于 2020-06-25 23:17:50
你可以用.map('{:,}'.format)
In [191]: df['Amount'].map('{:,}'.format)
Out[191]:
0 1,000,000.0
1 97,320.25
2 9,000,000.1
Name: Amount, dtype: object
In [192]: df['Amount'].map('{:,} $'.format)
Out[192]:
0 1,000,000.0 $
1 97,320.25 $
2 9,000,000.1 $
Name: Amount, dtype: objectIn [193]: df['Currency #1'] = df['Amount'].map('{:,}'.format)
In [194]: df['Currency #2'] = df['Amount'].map('{:,} $'.format)
In [195]: df
Out[195]:
Amount Currency #1 Currency #2
0 1000000.00 1,000,000.0 1,000,000.0 $
1 97320.25 97,320.25 97,320.25 $
2 9000000.10 9,000,000.1 9,000,000.1 $https://stackoverflow.com/questions/62585650
复制相似问题