Search code examples
pysparkrename

How to rename columns starting with 20 dynamically Pyspark


I have columns in my dataframe like this where the columns starting with 20 were generated dynamically.

enter image description here

I want to rename the columns starting with 20 to 2019_p, 2020_p, 2021_p dynamically.

How do I achieve this?


Solution

  • This should work:

    df.select(*[col(c).alias(f"${c}_p") if c.startswith("20") else col(c) for c in df.columns])