Reputation: 7583
This is a total noob question, sorry for that. In Spark, I can use select as:
df.select("*"); //to select everything
df.select(df.col("colname")[, df.col("colname")]); //to select one or more columns
df.select(df.col("colname"), df.col("colname").plus(1)) //to select a column and a calculated column
But. How can I select all the columns PLUS a calculated one? Obviously
select("*", df.col("colname").plus(1))
doesn't work (compilation error). How can this be done under JAVA?
Thank you!
Upvotes: 10
Views: 43114
Reputation: 7833
The difference between .select() and .withColumn() methods is that .select() returns only the columns you specify, while .withColumn() returns all the columns of the DataFrame in addition to the one you defined.
You can directly use withColumn:
df.withColumn("ColName", col("colName").plus(1))
Upvotes: 1
Reputation: 41
You can use withColumn()
method, this will create a new column to the DataFrame.
df.select("*")
.withColumn("ColName", col("colName").plus(1))
Upvotes: 4