pyspark dataframe filter or include based on list

后端 未结 3 815
自闭症患者
自闭症患者 2020-11-29 03:42

I am trying to filter a dataframe in pyspark using a list. I want to either filter based on the list or include only those records with a value in the list. My code below

3条回答
  •  隐瞒了意图╮
    2020-11-29 04:23

    what it says is "df.score in l" can not be evaluated because df.score gives you a column and "in" is not defined on that column type use "isin"

    The code should be like this:

    # define a dataframe
    rdd = sc.parallelize([(0,1), (0,1), (0,2), (1,2), (1,10), (1,20), (3,18), (3,18), (3,18)])
    df = sqlContext.createDataFrame(rdd, ["id", "score"])
    
    # define a list of scores
    l = [10,18,20]
    
    # filter out records by scores by list l
    records = df.filter(~df.score.isin(l))
    # expected: (0,1), (0,1), (0,2), (1,2)
    
    # include only records with these scores in list l
    df.where(df.score.isin(l))
    # expected: (1,10), (1,20), (3,18), (3,18), (3,18)
    

提交回复
热议问题