我有一个pyspark dataframe(df1)
,其第一第一行如下:
[Row(_c0='{"type":"Fi","values":[0.20100994408130646,1.172734797000885,0.06788740307092667,0.2314232587814331,0.2012220323085785]}', _c1='0')]
我想将“值”列表与dataframe(df2)
值以下的第一列进行比较,如下所示:
0 0.57581 1.25461 0.68694 0.974580 1.54789 0.23646
1 0.98745 0.23655 2.58970 4.587580 0.89756 1.25678
2 0.45780 5.78940 0.65986 2.125400 0.98745 1.23658
3 2.56834 0.25698 4.26587 0.569872 0.36987 0.68975
4 0.25678 1.23654 5.68320 0.986230 0.87563 2.58975
类似地,我在df1
中有很多行,我必须查看df1
“值”列表中的哪些值大于df2
中的相应列。我需要找到那些索引满足上述条件,并将其作为列表存储在df1
的另一列中。
例如,1.172737 > 0.98745
的索引为1
。因此,我将在df1 named(indices)
中有另一列,其中包含value1,如果出现另一个值,则必须追加该列。
比较是在相应的列和行之间进行的。上面显示的df1行是第一行,因此必须与df2中的第一列进行比较。
如果我对某事不重视,请在评论中让我知道。
答案 0 :(得分:0)
此代码可用于Python 2.7和Spark 2.3.2:
from pyspark.sql import functions as F
from pyspark.sql.types import ArrayType, IntegerType
# Create test dataframes
df1 = spark.createDataFrame([
['{"type":"Fi","values":[0.20100994408130646,1.172734797000885,0.06788740307092667,0.2314232587814331,0.2012220323085785]}', '0'],
['{"type":"Fi","values":[0.6, 0.8, 0.5, 2.1, 0.4]}', '0']
],['_c0','_c1'])
df2 = spark.createDataFrame([
[0, 0.57581, 1.25461, 0.68694, 0.974580, 1.54789, 0.23646],
[1, 0.98745, 0.23655, 2.58970, 4.587580, 0.89756, 1.25678],
[2, 0.45780, 5.78940, 0.65986, 2.125400, 0.98745, 1.23658],
[3, 2.56834, 0.25698, 4.26587, 0.569872, 0.36987, 0.68975],
[4, 0.25678, 1.23654, 5.68320, 0.986230, 0.87563, 2.58975]
],['id','v1', 'v2', 'v3', 'v4', 'v5', 'v6'])
# Get schema and load json correctly
json_schema = spark.read.json(df1.rdd.map(lambda row: row._c0)).schema
df1 = df1.withColumn('json', F.from_json('_c0', json_schema))
# Get column 1 values to compare
values = [row['v1'] for row in df2.select('v1').collect()]
# Define udf to compare values
def cmp_values(lst):
list_cmp = map(lambda t: t[0] > t[1], zip(lst, values)) # Boolean list
return [idx for idx, cond in enumerate(list_cmp) if cond] # Indices of satisfying elements
udf_cmp_values = F.udf(cmp_values, ArrayType(IntegerType()))
# Apply udf on array
df1 = df1.withColumn('indices', udf_cmp_values(df1.json['values']))
df1.show()
+--------------------+---+--------------------+---------+
| _c0|_c1| json| indices|
+--------------------+---+--------------------+---------+
|{"type":"Fi","val...| 0|[Fi, [0.201009944...| [1]|
|{"type":"Fi","val...| 0|[Fi, [0.6, 0.8, 0...|[0, 2, 4]|
+--------------------+---+--------------------+---------+