我在df中注册了一个tmp表,在列header中有空格。我可以在通过sqlContext使用sql查询时提取列。 我尝试使用后退但是它无法正常工作
df1 = sqlContext.sql("""select Company, Sector, Industry, `Altman Z-score as Z_Score` from tmp1 """)
答案 0 :(得分:5)
您只需将列名放在反向标记中,而不是其别名:
没有别名:
df1 = sqlContext.sql("""select Company, Sector, Industry, `Altman Z-score` as Z_Score from tmp1""")
使用别名:
df1 = sqlContext.sql("""select t1.Company, t1.Sector, t1.Industry, t1.`Altman Z-score` as Z_Score from tmp1 t1""")
答案 1 :(得分:2)
查询存在问题,更正后的查询在下面(包装为“中的Z_Score): -
df1 = sqlContext.sql("""select Company, Sector, Industry, `Altman Z-score` as Z_Score from tmp1 """)
另一个替代: -
import pyspark.sql.functions as F
df1 = sqlContext.sql("""select * from tmp1 """)
df1.select(F.col("Altman Z-score").alias("Z_Score")).show()