How to get name of dataframe column in pyspark?

后端 未结 5 1013
深忆病人
深忆病人 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:08

    The only way is to go an underlying level to the JVM.

    df.col._jc.toString().encode('utf8')
    

    This is also how it is converted to a str in the pyspark code itself.

    From pyspark/sql/column.py:

    def __repr__(self):
        return 'Column<%s>' % self._jc.toString().encode('utf8')
    

提交回复
热议问题