简体   繁体   中英

How can I pass a list of columns to select in pyspark dataframe?

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!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM