简体   繁体   English

Pyspark:Select 除特定列外的所有列

[英]Pyspark: Select all columns except particular columns

I have a large number of columns in a PySpark dataframe, say 200. I want to select all the columns except say 3-4 of the columns.我在 PySpark dataframe 中有大量列,比如 200。我想 select 除了说 3-4 列之外的所有列。 How do I select this columns without having to manually type the names of all the columns I want to select?我如何 select 这个列而不必手动键入我想要 select 的所有列的名称?

In the end, I settled for the following :最后,我解决了以下问题:

  • Drop : 掉落

    df.drop('column_1', 'column_2', 'column_3')

  • Select : 选择

    df.select([c for c in df.columns if c not in {'column_1', 'column_2', 'column_3'}])

df.drop(*[cols for cols in [list of columns to drop]])

Useful if the list to drop columns is huge.如果要删除列的列表很大,则很有用。 or if the list can be derived programmatically.或者该列表是否可以通过编程方式派生。

I have a large number of columns in a PySpark dataframe, say 200. I want to select all the columns except say 3-4 of the columns.我在 PySpark 数据框中有大量列,比如 200。我想选择除 3-4 列之外的所有列。 How do I select this columns without having to manually type the names of all the columns I want to select?如何选择此列而不必手动键入要选择的所有列的名称?

this might be helpful这可能会有所帮助

df_cols = list(set(df.columns) - {'<col1>','<col2>',....})

df.select(df_cols).show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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