如何删除"。"从Spark DataFrame列名?
DataFrame.select(F.col().alias())
方法重命名具有"。"的列名。在他们中引发错误。
以下代码是可重现的。
# import Spark libraries, configuration, Contexts, and types.
import pyspark
from pyspark.sql import SparkSession
import pyspark.sql.functions as F
#############
# Start Spark.
spark = SparkSession.builder.appName("test").getOrCreate()
testdf = spark.createDataFrame([
(1, "Julie", "CEO"),
(2, "Janice", "CFO"),
(3, "Jake", "CTO")],
["ID", "First Name", "Title Initial."])
# this works just fine.
testdf.select(F.col('First Name').alias('first_name')).show(3)
# This throws an error.
testdf.select(F.col('Title Initial.').alias('title')).show(3)
错误:
AnalysisException: u'syntax error in attribute name: Title Initial.;'
更改具有"。"的DataFrame列名称的替代方法是什么?在他们身上?
答案 0 :(得分:1)
使用testdf.select(F.col('`Title Initial.`').alias('title')).show(3)
{{1}}