【问题标题】:How can I pass a list of columns to select in pyspark dataframe?如何在 pyspark 数据框中传递要选择的列列表?
【发布时间】:2020-03-21 03:35:22
【问题描述】:

我有列表列名。

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

我想将该列表值作为“选择”数据框中的列名传递。

我试过了……

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

但我收到以下错误..

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.

你有什么想法吗? 谢谢各位!

【问题讨论】:

    标签: python apache-spark pyspark


    【解决方案1】:

    columns 之前使用* 以取消嵌套列列表并在.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)
    

    【讨论】:

      猜你喜欢
      • 2018-03-30
      • 1970-01-01
      • 2021-02-26
      • 2023-02-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      • 2019-03-04
      • 2019-11-25
      相关资源
      最近更新 更多