If I read data from a CSV, all the columns will be of "String" type by default. Generally, I inspect the data using the following functions which gives an overview of the data and its types
df.dtypes
df.show()
df.printSchema()
df.distinct().count()
df.describe().show()
But, if there is a column that I believe is of a particular type e.g. Double, I cannot be sure if all the values are double if I don't have business knowledge and because
- I cannot see all the values (millions of unique values)
- If I explicitly cast it to double type, spark quietly converts the type without throwing any exception and the values which are not double are converted to "null" - for example
Code:
from pyspark.sql.types import DoubleType
changedTypedf = df_original.withColumn('label', df_control_trip['id'].cast(DoubleType()))
What could be the best way to confirm the type of column then?
Counter([type(a) for a in df_control_trip['id']])
which will return something likeCounter({float: 4, int: 1087, str: 1035})
. When I usepandas.csv_read()
if the values are actually numbers, they will be read as float or int instead of str. $\endgroup$