如何比较两个数据帧并标记一个数据帧?
how to compare two dataframes and flag one data frame?
您好,我有两个如下所示的数据帧,并试图获得如下所示的结果数据帧。
只想比较关于 ID 列的数据帧。
id name item price
1 abc pen 10
2 bcd pencil 10
3 cde book 100
4 def stick 50
5 abc pencil 10
id name item price
2 xyz pen 10
50 ahjl phone 1000
1 fff mouse 200
5 ank stamp 20
49 anve cable 2000
结果table
id name item price flag
2 xyz pen 10 yes
5 ank stamp 20 yes
1 fff mouse 200 yes
50 ahjl phone 1000 no
49 anve cable 2000 no
我能够使用 python pandas 实现此目的。
你能帮我用 pyspark 做这件事吗?
谢谢,
安库什雷迪
假设你的数据帧分别被称为df1
、df2
:
import pyspark.sql.functions as F
df2.join(
df1.selectExpr("id", "'yes' as flag").dropDuplicates(),
["id"], "left"
).withColumn("flag", F.coalesce(F.col("flag"), F.lit("no"))).show()
+---+-----+----+-----+----+
| id| item|name|price|flag|
+---+-----+----+-----+----+
| 50|phone|ahjl| 1000| no|
| 5|stamp| ank| 20| yes|
| 1|mouse| fff| 200| yes|
| 49|cable|anve| 2000| no|
| 2| pen| xyz| 10| yes|
+---+-----+----+-----+----+
详情:
- 使用常量
yes
; 为 df1
预填充 flag
列
- 加入
df2
,并用 no
; 替换 flag
列中的 null
您好,我有两个如下所示的数据帧,并试图获得如下所示的结果数据帧。
只想比较关于 ID 列的数据帧。
id name item price
1 abc pen 10
2 bcd pencil 10
3 cde book 100
4 def stick 50
5 abc pencil 10
id name item price
2 xyz pen 10
50 ahjl phone 1000
1 fff mouse 200
5 ank stamp 20
49 anve cable 2000
结果table
id name item price flag
2 xyz pen 10 yes
5 ank stamp 20 yes
1 fff mouse 200 yes
50 ahjl phone 1000 no
49 anve cable 2000 no
我能够使用 python pandas 实现此目的。 你能帮我用 pyspark 做这件事吗?
谢谢,
安库什雷迪
假设你的数据帧分别被称为df1
、df2
:
import pyspark.sql.functions as F
df2.join(
df1.selectExpr("id", "'yes' as flag").dropDuplicates(),
["id"], "left"
).withColumn("flag", F.coalesce(F.col("flag"), F.lit("no"))).show()
+---+-----+----+-----+----+
| id| item|name|price|flag|
+---+-----+----+-----+----+
| 50|phone|ahjl| 1000| no|
| 5|stamp| ank| 20| yes|
| 1|mouse| fff| 200| yes|
| 49|cable|anve| 2000| no|
| 2| pen| xyz| 10| yes|
+---+-----+----+-----+----+
详情:
- 使用常量
yes
; 为 - 加入
df2
,并用no
; 替换
df1
预填充 flag
列
flag
列中的 null