pyspark-sql,列名中包含破折号/连字符

yrwegjxp  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(438)

我有PypSparkDataframe

data = {'Passenger-Id': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5},'Age': {0: 22, 1: 38, 2: 26, 3: 35, 4: 35}}
df_pd = pd.DataFrame(data, columns=data.keys())
df = spark.createDataFrame(df_pd)
+------------+---+
|Passenger-Id|Age|
+------------+---+
|           1| 22|
|           2| 38|
|           3| 26|
|           4| 35|
|           5| 35|
+------------+---+

这很管用

df.filter(df.Age == 22).show()

但下面不起作用,因为-在列名中

df.filter(df.Passenger-Id == 2).show()

attributeerror:“dataframe”对象没有属性“passenger”
我在spark sql也面临同样的问题,

spark.sql("SELECT  Passenger-Id FROM AutoMobile").show()

        spark.sql("SELECT  automobile.Passenger-Id FROM AutoMobile").show()

低于错误
analysisexception:无法解析' Passenger '给定的输入列:[automobile.age,automobile.passenger id]
正如一些资料中建议的那样,尝试用单引号表示列名,现在只打印查询中提到的列

spark.sql("SELECT  'Passenger-Id' FROM AutoMobile").show()
+------------+
|Passenger-Id|
+------------+
|Passenger-Id|
|Passenger-Id|
|Passenger-Id|
|Passenger-Id|
|Passenger-Id|
+------------+
eh57zj3b

eh57zj3b1#

既然您在列名中有hiphen,我建议您使用 col() 函数来自 sql.functions ```
import pyspark.sql.functions as F
df.filter(F.col('Passenger-Id')== 2).show()

这是结果

+------------+---+
|Passenger-Id|Age|
+------------+---+
| 2| 38|
+------------+---+

对于sql语法,您需要使用特殊字符“`”而不是单引号,如下所示

df.createOrReplaceTempView("AutoMobile")
spark.sql("SELECT * FROM AutoMobile where Passenger-Id=2").show()

相关问题