cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

how to check dataframe column value

databicky
Contributor II

in my dataframe it have one column name like count, if that particular column value is greater than zero, the job needs to get failed, how can i perform that one?​

1 ACCEPTED SOLUTION

Accepted Solutions

Hubert-Dudek
Esteemed Contributor III

just put like in SQL

"colname > 0 OR colname< 0"

or

"colname != 0"

View solution in original post

6 REPLIES 6

Chaitanya_Raju
Honored Contributor

Hi @Mohammed sadamusean​ 

Can you try like below code in pyspark and let me know if you face any issues

variable_name = df.select(col("Column_Name")).collect()[0][0]
 
if(variable_name>0):
dbutils.notebook.exit('Notebook Failed')  

Happy Learning!!

Thanks for reading and like if this is useful and for improvements or feedback please comment.

Hubert-Dudek
Esteemed Contributor III

Code without collect, which should not be used in production:

if df.filter("count > 0").count() > 0: dbutils.notebook.exit('Notebook Failed')

you can also use a more aggressive version:

if df.filter("count > 0").count() > 0: raise Exception("count bigger than 0")

but it will get total count of the column right​, but i need to check every specific column value

Hubert-Dudek
Esteemed Contributor III

first you filter for rows matching your query. You said that column is named count. Let's assume that column is called col instead, so filter("col > 0"), and then you apply the count() function, which will return how many rows match those criteria.

it is working but ​how can we check the columns based on two values like count >0 and less than 0 , i tried with equal to 0 but it doesn't worked

Hubert-Dudek
Esteemed Contributor III

just put like in SQL

"colname > 0 OR colname< 0"

or

"colname != 0"

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group