Alan
Alan

Reputation: 469

How to get datatype of a column in Scala dataframe

How can I get the datatype of column from a dataframe in Scala. I found some answers here and there but non of them are helpful and exact.

For example there is column X with data type (StringStype) and after doing:

     scala> df.select("X").dtypes
     res: Array[(String, String)] = Array((X,StringType))

I am not sure why it returns array while it is only a string.

What I am looking for to exactly return "StringType" without any extra information and cleaned to be able to play with it.

Upvotes: 0

Views: 705

Answers (1)

Alan
Alan

Reputation: 469

That's what I was looking for:

 scala> df.select("X").dtypes(0)._2
 res: String = StringType

At least closer to what I need and cleaner to play with.

Upvotes: 1

Related Questions