How to get name of dataframe column in pyspark?

后端 未结 5 1022
深忆病人
深忆病人 2021-02-01 13:44

In pandas, this can be done by column.name.

But how to do the same when its column of spark dataframe?

e.g. The calling program has a spark dataframe: spark_df

5条回答
  •  面向向阳花
    2021-02-01 14:03

    Python

    As @numeral correctly said, column._jc.toString() works fine in case of unaliased columns.

    In case of aliased columns (i.e. column.alias("whatever") ) the alias can be extracted, even without the usage of regular expressions: str(column).split(" AS ")[1].split("`")[1] .

    I don't know Scala syntax, but I'm sure It can be done the same.

提交回复
热议问题