我有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|
+------------+
1条答案
按热度按时间eh57zj3b1#
既然您在列名中有hiphen,我建议您使用
col()
函数来自sql.functions
```import pyspark.sql.functions as F
df.filter(F.col('Passenger-Id')== 2).show()
+------------+---+
|Passenger-Id|Age|
+------------+---+
| 2| 38|
+------------+---+
df.createOrReplaceTempView("AutoMobile")
spark.sql("SELECT * FROM AutoMobile where
Passenger-Id
=2").show()