Skip to content

Instantly share code, notes, and snippets.

@CsBigDataHub
Forked from vgiri2015/ColumnConversion.py
Created March 25, 2018 16:48
Show Gist options
  • Save CsBigDataHub/d7764cd7e5f7e96437dcd84e6808f205 to your computer and use it in GitHub Desktop.
Save CsBigDataHub/d7764cd7e5f7e96437dcd84e6808f205 to your computer and use it in GitHub Desktop.
df = sc.parallelize([(1, 'Y','F',"Giri",'Y'), (2, 'N','V',"Databricks",'N'),(3,'Y','B',"SparkEdge",'Y'),(4,'N','X',"Spark",'N')]).toDF(["id", "flag1","flag2","name","flag3"])
print 'Show Dataframe'
df.show()
print 'Actual Schema of the df'
df.printSchema()
for a_dftype in df.dtypes:
col_name = a_dftype[0]
col_type = a_dftype[1]
# print df.select(col_name).collect()[0][0]
if col_type=='string' and (df.select(col_name).distinct().collect()[0][0] =='N' or df.select(col_name).distinct().collect()[0][0] =='Y'):
df = df.withColumn(col_name,df[col_name].cast("boolean")).drop(df[col_name])
else:
df = df.withColumn(col_name,df[col_name]).drop(df[col_name])
print 'df with True/False Value after Data Type changes'
df.show()
print 'Modified Schema of the df'
df.printSchema()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment