I have dataframe in which some column start with number and when I create new column with dict of all column and row value using below code. I am not geeting correct output.
| Name | Date | 1Y | 2Y |
|---|---|---|---|
| XYZ | 2022-05-22 | 0.0898 | 0.9090 |
After running this code
df = df.withColumn("map", F.expr("map(" + ",".join([f"'{c}', {c}" for c in df.columns]) + ")"))
I am getting
| Name | Date | 1Y | 2Y | map |
|---|---|---|---|---|
| XYZ | 2022-05-22 | 0.0898 | 0.9090 | {"Name":"XYZ","Date":2022-05-22","1Y":"1","2Y":"2"} |
but when I rename the columns which start with number like 1Y to Y1 and 2Y to Y2 I am geeting the correct output like below
| Name | Date | Y1 | Y2 | map |
|---|---|---|---|---|
| XYZ | 2022-05-22 | 0.0898 | 0.9090 | {"Name":"XYZ","Date":2022-05-22","Y1":"0.0898","Y2":"0.9090"} |
is there any way to do this without renaming the column? I have many columns starting with numbers.