Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
Find column index by searching column header of a Dataset in Apache Spark Java

I have a Spark Dataset similar to the example below:

       0         1                  2          3
    +------+------------+--------------------+---+
    |ItemID|Manufacturer|       Category     |UPC|
    +------+------------+--------------------+---+
    |   804|         ael|Brush & Broom Han...|123|
    |   805|         ael|Wheel Brush Parts...|124|
    +------+------------+--------------------+---+

I need to find the position of a column by searching the column header.

For Example:

int position=getColumnPosition("Category");

This should return 2.

Is there any Spark function supported on Dataset<Row> datatype to find the column index or any java functions which can run on Spark dataset?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You need to access the schema and read the field index as follows:

int position = df.schema().fieldIndex("Category");
about 4 years ago · Santiago Trujillo Report

0

I have used index attribute for lists:

df.columns.index(column_name)
about 4 years ago · Santiago Trujillo Report

0

You can consider this option (Scala implementation):

def getColumnPosition(dataframe: DataFrame, colName: String): Int = {
    dataframe.columns.indexOf(colName)
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!