0

I have a scenario here - Have 30 columns in one dataframe, need to drop specific column and select remaining columns and put it to another dataframe. How can I achieve this? I tried below.

val df1: DataFrame = df2.as(a).join( df3.as(b),col(a.key) === col(b.key), inner).drop(a.col1)
.select(" a.star ")

when I do show of df1, its still show col1. Any advise on resolving this.

2
  • What are the columns in dataframe a and b? Commented Jun 2, 2021 at 5:13
  • dataframe a has 30 columns (1 to 30) and b has 1 column and need to select 29 columns from a except 1 or any column. Sorry, if I am unclear from above post. Commented Jun 2, 2021 at 5:49

2 Answers 2

1

drop requires a string without table alias, so you can try:

val df1 = df2.as("a")
    .join(df3.as("b"), col("a.key") === col("b.key"), "inner")
    .drop("col1")
    .select("a.*")

Or instead of dropping the column, you can filter the columns to be selected:

val df1 = df2.as("a")
    .join(df3.as("b"), col("a.key") === col("b.key"), "inner")
    .select(df2.columns.filterNot(_ == "col1").map("a." + _): _*)
Sign up to request clarification or add additional context in comments.

Comments

0

This really just seems like you need to use a "left_semi" join.

val df1 = df2.drop('col1).join(df3, df2("key") === df3("key"), "left_semi")

If key is an actual column you can simplify the syntax even further

val df1 = df2.drop('col1).join(df3, Seq("key"), "left_semi")

The best syntax depends on the details of what your real data looks like. If you need to refer to col1 in df2 specifically because there's ambiguity, then use df2("col1")

left_semi joins take all the columns from the left table for rows finding a match in the right table.

1 Comment

Thanks mck & kanielc for advise and approaches, I achieved this like below. val df1: DataFrame = df2.as(a).join( df3.as(b),col(a.key) === col(b.key), inner) .select(col(" a.* ")).drop(col(a.col1)). This gives result all columns of df2 except col1 (droped col1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.