Quantcast
Channel: How can I change column types in Spark SQL's DataFrame? - Stack Overflow
Browsing all 24 articles
Browse latest View live

Answer by remigiusz boguszewicz for How can I change column types in Spark...

Why not just do as described under http://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.Column.castdf.select(df.year.cast("int"),"make","model","comment","blank")

View Article



Answer by Ravi for How can I change column types in Spark SQL's DataFrame?

In case if you want to change multiple columns of a specific type to another without specifying individual column names/* Get names of all columns that you want to change type. In this example I want...

View Article

Answer by Mehdi LAMRANI for How can I change column types in Spark SQL's...

So many answers and not much thorough explanationsThe following syntax works Using Databricks Notebook with Spark 2.4from pyspark.sql.functions import *df = df.withColumn("COL_NAME",...

View Article

Answer by Piyush Patel for How can I change column types in Spark SQL's...

I think this is lot more readable for me.import org.apache.spark.sql.types._df.withColumn("year", df("year").cast(IntegerType))This will convert your year column to IntegerType with creating any...

View Article

Answer by Vibha for How can I change column types in Spark SQL's DataFrame?

Another solution is as follows:1) Keep "inferSchema" as False2) While running 'Map' functions on the row, you can read 'asString' (row.getString...)//Read CSV and create datasetDataset<Row>...

View Article


Answer by cubic lettuce for How can I change column types in Spark SQL's...

In case you have to rename dozens of columns given by their name, the following example takes the approach of @dnlbrky and applies it to several columns at once:df.selectExpr(df.columns.map(cn => {...

View Article

Answer by Eric Bellet for How can I change column types in Spark SQL's...

Using Spark Sql 2.4.0 you can do that:spark.sql("SELECT STRING(NULLIF(column,'')) as column_string")

View Article

Answer by user8106134 for How can I change column types in Spark SQL's...

Another way:// Generate a simple dataset containing five values and convert int to string typeval df = spark.range(5).select( col("id").cast("string")).withColumnRenamed("id","value")

View Article


Answer by user8106134 for How can I change column types in Spark SQL's...

Generate a simple dataset containing five values and convert int to string type:val df = spark.range(5).select( col("id").cast("string") )

View Article


Answer by adarsh for How can I change column types in Spark SQL's DataFrame?

You can use below code.df.withColumn("year", df("year").cast(IntegerType))Which will convert year column to IntegerType column.

View Article

Answer by PirateJack for How can I change column types in Spark SQL's DataFrame?

This method will drop the old column and create new columns with same values and new datatype. My original datatypes when the DataFrame was created were:-root |-- id: integer (nullable = true) |--...

View Article

Answer by Tejasvi Sharma for How can I change column types in Spark SQL's...

One can change data type of a column by using cast in spark sql.table name is table and it has two columns only column1 and column2 and column1 data type is to be changed.ex-spark.sql("select...

View Article

Answer by Aravind Krishnakumar for How can I change column types in Spark...

val fact_df = df.select($"data"(30) as "TopicTypeId", $"data"(31) as "TopicId",$"data"(21).cast(FloatType).as( "Data_Value_Std_Err")).rdd //Schema to be applied to the table val fact_schema = (new...

View Article


Answer by soulmachine for How can I change column types in Spark SQL's...

df.select($"long_col".cast(IntegerType).as("int_col"))

View Article

Answer by sauraI3h for How can I change column types in Spark SQL's DataFrame?

the answers suggesting to use cast, FYI, the cast method in spark 1.4.1 is broken.for example, a dataframe with a string column having value "8182175552014127960" when casted to bigint has value...

View Article


Answer by manishbelsare for How can I change column types in Spark SQL's...

Java code for modifying the datatype of the DataFrame from String to Integerdf.withColumn("col_name", df.col("col_name").cast(DataTypes.IntegerType))It will simply cast the existing(String datatype) to...

View Article

Answer by ben jarman for How can I change column types in Spark SQL's DataFrame?

So this only really works if your having issues saving to a jdbc driver like sqlserver, but it's really helpful for errors you will run into with syntax and types.import...

View Article


Answer by msemelman for How can I change column types in Spark SQL's DataFrame?

Edit: Newest newest versionSince spark 2.x you should use dataset api instead when using Scala [1]. Check docs...

View Article

Answer by Martin Senne for How can I change column types in Spark SQL's...

As the cast operation is available for Spark Column's (and as I personally do not favour udf's as proposed by @Svend at this point), how about:df.select( df("year").cast(IntegerType).as("year"), ......

View Article

Answer by WeiChing 林煒清 for How can I change column types in Spark SQL's...

First, if you wanna cast type, then this:import org.apache.spark.sqldf.withColumn("year", $"year".cast(sql.types.IntegerType))With same column name, the column will be replaced with new one. You don't...

View Article
Browsing all 24 articles
Browse latest View live




Latest Images