>我有一个Pyspark数据框,如下所示:
+------------+-------------+--------------------+
|package_id | location | package_scan_code |
+------------+-------------+--------------------+
|123 | Denver |05 |
|123 | LosAngeles |03 |
|123 | Dallas |09 |
|123 | Vail |02 |
|456 | Jacksonville|05 |
|456 | Nashville |09 |
|456 | Memphis |03 |
"package_scan_code" 03 表示包裹的来源。
我想向此数据帧添加一个列"origin",以便对于每个包(由"package_id"标识),新添加的 origin 列中的值将与"package_scan_code"03 对应的位置相同。
在上面的例子中,有两个独特的包 123 和 456,它们的起源分别是洛杉矶和孟菲斯(对应于 package_scan_code 03)。
所以我希望我的输出如下:
+------------+-------------+--------------------+------------+
| package_id |location | package_scan_code |origin |
+------------+-------------+--------------------+------------+
|123 | Denver |05 | LosAngeles |
|123 | LosAngeles |03 | LosAngeles |
|123 | Dallas |09 | LosAngeles |
|123 | Vail |02 | LosAngeles |
|456 | Jacksonville|05 | Memphis |
|456 | Nashville |09 | Memphis |
|456 | Memphis |03 | Memphis |
如何在 Pyspark 中实现这一点?我尝试了.withColumn
方法,但我无法获得正确的条件。
按package_scan_code == '03'
过滤数据框,然后重新连接原始数据框:
(df.filter(df.package_scan_code == '03')
.selectExpr('package_id', 'location as origin')
.join(df, ['package_id'], how='right')
.show())
+----------+----------+------------+-----------------+
|package_id| origin| location|package_scan_code|
+----------+----------+------------+-----------------+
| 123|LosAngeles| Denver| 05|
| 123|LosAngeles| LosAngeles| 03|
| 123|LosAngeles| Dallas| 09|
| 123|LosAngeles| Vail| 02|
| 456| Memphis|Jacksonville| 05|
| 456| Memphis| Nashville| 09|
| 456| Memphis| Memphis| 03|
+----------+----------+------------+-----------------+
注意:这假设您最多有一个package_scan_code
等于每package_id
03
,否则逻辑将不正确,您需要重新考虑如何定义origin
。
数据帧中每个package_id
发生package_scan_code=03
的次数如何,此代码都应有效。我又加了一个(123,'LosAngeles','03')
来证明——
步骤 1:创建数据帧
values = [(123,'Denver','05'),(123,'LosAngeles','03'),(123,'Dallas','09'),(123,'Vail','02'),(123,'LosAngeles','03'),
(456,'Jacksonville','05'),(456,'Nashville','09'),(456,'Memphis','03')]
df = sqlContext.createDataFrame(values,['package_id','location','package_scan_code'])
第 2 步:创建package_id
和location
字典。
df_count = df.where(col('package_scan_code')=='03').groupby('package_id','location').count()
dict_location_scan_code = dict(df_count.rdd.map(lambda x: (x['package_id'], x['location'])).collect())
print(dict_location_scan_code)
{456: 'Memphis', 123: 'LosAngeles'}
第 3 步:创建列,映射字典。
from pyspark.sql.functions import col, create_map, lit
from itertools import chain
mapping_expr = create_map([lit(x) for x in chain(*dict_location_scan_code.items())])
df = df.withColumn('origin', mapping_expr.getItem(col('package_id')))
df.show()
+----------+------------+-----------------+----------+
|package_id| location|package_scan_code| origin|
+----------+------------+-----------------+----------+
| 123| Denver| 05|LosAngeles|
| 123| LosAngeles| 03|LosAngeles|
| 123| Dallas| 09|LosAngeles|
| 123| Vail| 02|LosAngeles|
| 123| LosAngeles| 03|LosAngeles|
| 456|Jacksonville| 05| Memphis|
| 456| Nashville| 09| Memphis|
| 456| Memphis| 03| Memphis|
+----------+------------+-----------------+----------+