2024-09-28 05:29:06 发布
网友
假设存在以下形式的pyspark数据帧:
id col1 col2 col3 col4 ------------------------ as1 4 10 4 6 as2 6 3 6 1 as3 6 0 2 1 as4 8 8 6 1 as5 9 6 6 9
有没有一种方法可以搜索pyspark数据帧的col2-4中的值,并返回(id行名,列名)? 例如:
提示:假设col1是一个集合{4,6,8,9},即唯一的
是的,您可以利用sparksql.isin运算符。在
.isin
让我们首先在示例中创建数据帧
第1部分-创建数据帧
cSchema = StructType([StructField("id", IntegerType()),\ StructField("col1", IntegerType()),\ StructField("col2", IntegerType()),\ StructField("col3", IntegerType()),\ StructField("col4", IntegerType())]) test_data = [[1,4,10,4,6],[2,6,3,6,1],[3,6,0,2,1],[4,8,8,6,1],[5,9,6,6,9]] df = spark.createDataFrame(test_data,schema=cSchema) df.show() + -+ + + + + | id|col1|col2|col3|col4| + -+ + + + + | 1| 4| 10| 4| 6| | 2| 6| 3| 6| 1| | 3| 6| 0| 2| 1| | 4| 8| 8| 6| 1| | 5| 9| 6| 6| 9| + -+ + + + +
第2部分-搜索匹配值的函数
isin:一个布尔表达式,如果该表达式的值包含在参数的计算值中,则该表达式的计算结果为true。 http://spark.apache.org/docs/2.1.0/api/python/pyspark.sql.html
这会指引你正确的方向。您可以只选择Id列等。。或者你想要归还的东西。可以很容易地更改该函数,以获取更多的列进行搜索。希望这有帮助!在
# create structfield using array list cSchema = StructType([StructField("id", StringType()), StructField("col1", IntegerType()), StructField("col2", IntegerType()), StructField("col3", IntegerType()), StructField("col4", IntegerType())]) test_data = [['as1', 4, 10, 4, 6], ['as2', 6, 3, 6, 1], ['as3', 6, 0, 2, 1], ['as4', 8, 8, 6, 1], ['as5', 9, 6, 6, 9]] # create pyspark dataframe df = spark.createDataFrame(test_data, schema=cSchema) df.show() # obtain the distinct items for col 1 distinct_list = [i.col1 for i in df.select("col1").distinct().collect()] # rest columns col_list = ['id', 'col2', 'col3', 'col4'] # implement the search of values in rest columns found in col 1 def search(distinct_list ): for i in distinct_list : print(str(i) + ' found in: ') # for col in df.columns: for col in col_list: df_search = df.select(*col_list) \ .filter(df[str(col)] == str(i)) if (len(df_search.head(1)) > 0): df_search.show() search(distinct_list)
在GITHUB处查找完整的示例代码
是的,您可以利用sparksql
.isin
运算符。在让我们首先在示例中创建数据帧
第1部分-创建数据帧
第2部分-搜索匹配值的函数
isin:一个布尔表达式,如果该表达式的值包含在参数的计算值中,则该表达式的计算结果为true。 http://spark.apache.org/docs/2.1.0/api/python/pyspark.sql.html
^{pr2}$这会指引你正确的方向。您可以只选择Id列等。。或者你想要归还的东西。可以很容易地更改该函数,以获取更多的列进行搜索。希望这有帮助!在
在GITHUB处查找完整的示例代码
相关问题 更多 >
编程相关推荐