from pyspark.sql import SparkSession from pyspark.sql.types import StructType, StructField, StringType from pyspark.sql.functions import when
spark = SparkSession.builder
.appName("Example")
.getOrCreate()
schema = StructType([ StructField("country_code", StringType(), True) ])
data = [("IN",), ("SG",), ("US",)] columns = ["country_code"]
df = spark.createDataFrame(data, schema=schema) final_df = df.withColumn("country_name", when(df["country_code"] == "IN", "India") .when(df["country_code"] == "SG", "Singapore") .when(df["country_code"] == "US", "United States"))
final_df.display()