'AttributeError: 'str' object has no attribute 'filter'

Can anyone help me with this piece if code I'm getting the error

AttributeError: 'str' object has no attribute 'filter'

table_sample = table_sample.filter(col('project_type').isin('1','2','3')) 

table_sample=("select class,name,subject from (select class,name,subject OVER (PARTITION BY class ORDER BY subject DESC) as rnk from table_student%s) as rnk from table_student%s)  where a.rnk = 1"%(somevariable))


Solution 1:[1]

If I understand correctly, you're trying to generate a dynamic query using string literals. Then you want to assign its output to table_sample, and then filter the table_sample.
You have to execute the string that contains your query using spark.sql("<string of query>").

table_sample_df = spark.sql(table_sample)
table_sample_df = table_sample_df.filter(col('project_type').isin('1','2','3'))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ARCrow