5

Data frame showing _c0,_c1 instead my original column names in first row.
i want to show My column name which is on first row of my CSV.

    dff = 
    spark.read.csv("abfss://dir@acname.dfs.core.windows.net/
    diabetes.csv")
    dff:pyspark.sql.dataframe.DataFrame
    _c0:string
    _c1:string
    _c2:string
    _c3:string
    _c4:string
    _c5:string
    _c6:string
    _c7:string
    _c8:string
Gaurav Gangwar
  • 467
  • 3
  • 11
  • 24
  • 4
    Use `header=True` – pault Aug 01 '19 at 12:47
  • 1
    Possible duplicate of [Load CSV file with Spark](https://stackoverflow.com/questions/28782940/load-csv-file-with-spark) (the most upvoted answer, not the accepted one) – pault Aug 01 '19 at 12:48

3 Answers3

9

Very simple solution is to have a header=True while you read the file:

dff = spark.read.csv("abfss://dir@acname.dfs.core.windows.net/diabetes.csv", header=True)
Kishan Vyas
  • 126
  • 2
  • If headers are indeed blank I want it to remain blank instead of appending it with _c0 etc., do we have any fix for this? – Scope Jan 05 '22 at 13:45
2

Set header as true while loading the CSV file.

spark.read.format("csv")
                   .option("delimiter", ",")
                   .option("header", "true")
                   .option("inferSchema", "true")
                   .load("file.csv")
Aman Sehgal
  • 546
  • 4
  • 13
-1

I Just Sorted By below code

    .select(col("_c0").alias("A"),
             col("_c1").alias("B"),
             col("_c2").alias("C"),
             col("_c3").alias("D"),
             col("_c4").alias("E")

            )
zmag
  • 7,825
  • 12
  • 32
  • 42
Gaurav Gangwar
  • 467
  • 3
  • 11
  • 24