简体   繁体   English

从数据框火花中删除一列

[英]remove a column from a dataframe spark

I have a Spark dataframe with a very large number of columns.我有一个包含大量列的 Spark 数据框。 I want to remove two columns from it to get a new dataframe.我想从中删除两列以获取新的数据框。

Had there been fewer columns, I could have used the select method in the API like this:如果列较少,我可以像这样在 API 中使用 select 方法:

pcomments = pcomments.select(pcomments.col("post_id"),pcomments.col("comment_id"),pcomments.col("comment_message"),pcomments.col("user_name"),pcomments.col("comment_createdtime"));

But since picking columns from a long list is a tedious task, is there a workaround?但是由于从长列表中挑选列是一项繁琐的任务,是否有解决方法?

Use drop method and withColumnRenamed methods.使用drop方法和withColumnRenamed方法。

Example:例子:

    val initialDf= ....

    val dfAfterDrop=initialDf.drop("column1").drop("coumn2")

    val dfAfterColRename= dfAfterDrop.withColumnRenamed("oldColumnName","new ColumnName")

Try this:尝试这个:

val initialDf = ...

val dfAfterDropCols = initialDf.drop("column1", "coumn2")

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

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