Pyspark货币转换器

时间:2018-10-05 06:57:54

标签: dataframe pyspark currency

我有一个像这样的数据框df

df.show()

输出:

+-----+--------+----------+
|price|currency|      date|
+-----+--------+----------+
|   10|     USD|2018-07-03|
|   10|     USD|2018-03-19|
|    8|     SEK|2018-07-10|
|   10|     NOK|2018-05-25|
|    5|     EUR|2018-05-13|
+-----+--------+----------+

,我想根据指定的price将每个EUR转换为price_eur并放入列date中。

+-----+--------+----------+---------+
|price|currency|      date|price_eur|
+-----+--------+----------+---------+
|   10|     USD|2018-07-03|     8.57|
|   10|     USD|2018-03-18|     8.12|
|    8|     SEK|2018-07-10|     0.78|
|   10|     NOK|2018-05-25|     1.05|
|    5|     EUR|2018-05-13|        5|
+-----+--------+----------+---------+

有人知道这样做的有效方法吗?

在拥有pandas数据帧的情况下,我可以简单地使用CurrencyConverter python API,但是在pyspark中却找不到解决方法。

1 个答案:

答案 0 :(得分:2)

创建一个udf并使用相同的API。

-(void)showForm{
    condition = [[NSCondition alloc] init];
    self.parentViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
    [self.parentViewController.view setUserInteractionEnabled:NO];
    destinationViewController = [[ViewController alloc] initWithValues:self.filePath andCondition:condition andChoice:@"Camera"];
    [[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:destinationViewController animated:NO completion:nil];
    [condition lock];
    [condition wait];    
    self.filePath = [(ViewController*)destinationViewController getImagePath];
    [self.parentViewController.view setUserInteractionEnabled:YES];
    [condition unlock];



}

编辑:

首先使用以下命令安装python软件包,

from currency_converter import CurrencyConverter
import pyspark.sql.functions as F
from pyspark.sql.types import FloatType

c = CurrencyConverter()
convert_curr = F.udf(lambda x,y : c.convert(x, y, 'EUR'), FloatType())
df = df.withColumn('price_eur', convert_curr('price', 'currency'))