In this short how-to article, we will learn how to add an empty column to Pandas and PySpark DataFrames.
Pandas
We can create an empty column by assigning an empty string as follows:
df["f4"] = ""
PySpark
The logic is the same as Pandas but the implementation is a bit different. We can assign an empty string to a new column as follows:
from pyspark.sql import functions as F
df = df.withColumn("f4", F.lit(""))
This question is also being asked as:
- Add multiple empty columns to Pandas DataFrame