17

I have list column names.

columns = ['home','house','office','work']

and I would like to pass that list values as columns name in "select" dataframe.

I have tried it...

df_tables_full = df_tables_full.select('time_event','kind','schema','table',columns)

but I have received error below..

TypeError: Invalid argument, not a string or column: ['home', 'house', 'office',
'work'] of type <class 'list'>. For column literals, use 'lit', 'array', 'struct' 
or 'create_map' function.

Can you have any ideia? Thank you guys!

1 Answer 1

40

Use * before columns to unnest columns list and use in .select.

columns = ['home','house','office','work']

#select the list of columns
df_tables_full.select('time_event','kind','schema','table',*columns).show()

df_tables_full = df_tables_full.select('time_event','kind','schema','table',*columns)
Sign up to request clarification or add additional context in comments.

1 Comment

This only works from Python 3.5 onwards?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.