Description :
I have a list of column names which I need.
I want to check if all these columns names are present in dataframe.if some columns are present then use those columns and make a generic code like
Df1=df.select(df[column1],df(column2])
List=[column1,column2,column3,column4] Want to check if columns in list is present and whatever the columns are present in dataframe use it in select query
2
Answers
You need to do it in an iterative fashion
The other answer(s) work perfectly. But it can also be written in a one liner.
The list comprehension within the
select()
checks if any column fromdata_sdf
dataframe is present in thereqd_cols
list and keeps only the ones that are overlapping.