【发布时间】:2021-02-02 06:37:52
【问题描述】:
我想使用 Babel 和 PySpark 将 float 转换为 currency
样本数据:
amount currency
2129.9 RON
1700 EUR
1268 GBP
741.2 USD
142.08091153 EUR
4.7E7 USD
0 GBP
我试过了:
df = df.withColumn(F.col('amount'), format_currency(F.col('amount'), F.col('currency'),locale='be_BE'))
或
df = df.withColumn(F.col('amount'), format_currency(F.col('amount'), 'EUR',locale='be_BE'))
【问题讨论】:
-
您能否提供
amount列的样本数据,并且由于错误是由于十进制转换,所以您可以检查/在整个amount列中是否有任何/。 -
所以你是说整个
amount列中除了numeric之外没有其他字符了?因为没有其他原因会出现decimal conversion错误 -
我检查了表格末尾列中有一个
E,例如4.7E7 -
删除这些字符并重试。还要检查行是否为
numeric or integer,您可以使用df.select("amount",F.col("amount").cast("int").isNotNull().alias("Truth")).show() -
删除这些字符
regexp_extract的最佳方法是什么?
标签: apache-spark pyspark user-defined-functions currency-formatting python-babel