与SelectExpr条件的pyspark计数
我有一个带有“年龄”列的数据框架,例如,我想计算多少行= 60。我知道如何使用select或df.count()解决此问题,但是我想使用selectexpr。
我尝试了
customerDfwithAge.selectExpr("count(when(col(age) = 60))")
,但是
Undefined function: 'col'. This function is neither a registered temporary function nor a permanent function registered in the database 'default'.;
如果我尝试删除 col ,它会返回我,这会使我返回
Invalid arguments for function when; line 1 pos 6
什么问题?
I have a DataFrame with a column "age" and I want to count how many rows with age = 60, for example. I know how to solve this using select or df.count() but I want to use selectExpr.
I tried
customerDfwithAge.selectExpr("count(when(col(age) = 60))")
but it returns me
Undefined function: 'col'. This function is neither a registered temporary function nor a permanent function registered in the database 'default'.;
If I try to remove col, it returns me
Invalid arguments for function when; line 1 pos 6
What is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要使用
selectexpr
,则需要提供有效的SQL表达式。()()
和col()
是pyspark.sql.functions
而不是SQL表达式。在您的情况下,您应该尝试:
请记住,我正在使用
sum
不是count
。计数
将计算每个行(0
s和1
s),它将仅返回数据框架的行总数。If you want to use
selectExpr
you need to provide a valid SQL expression.when()
andcol()
arepyspark.sql.functions
not SQL expressions.In your case, you should try:
Bear in mind that I am using
sum
notcount
.count
will count every row (0
s and1
s) and it would simply return the total number of rows of your dataframe.