11

Pandas: Check if all values in column are zeros

 3 years ago
source link: https://thispointer.com/pandas-check-if-all-values-in-column-are-zeros/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Pandas: Check if all values in column are zeros

This article will discuss checking if all values in a DataFrame column are zero (0) or not.

First of all, we will create a DataFrame from a list of tuples,

import pandas as pd
# List of Tuples
students = [('jack', 34, 'Sydney', 'Australia', 0),
('Riti', 30, 'Delhi', 'India', 0),
('Vikas', 0, 'Mumbai', 'India', 0),
('Neelu', 0, 'Bangalore','India', 0),
('John', 16, 'New York', 'US', 0),
('Mike', 17, 'las vegas', 'US', 0)]
# Create a DataFrame object
df = pd.DataFrame( students,
columns=['Name', 'Age', 'City', 'Country', 'Budget'],
index=['a', 'b', 'c', 'd', 'e', 'f'])
# Display the DataFrame
print(df)
import pandas as pd

# List of Tuples
students = [('jack',    34, 'Sydney',   'Australia', 0),
            ('Riti',    30, 'Delhi',    'India',     0),
            ('Vikas',   0,  'Mumbai',   'India',     0),
            ('Neelu',   0,  'Bangalore','India',     0),
            ('John',    16, 'New York',  'US',       0),
            ('Mike',    17, 'las vegas', 'US',       0)]

# Create a DataFrame object
df = pd.DataFrame( students,
                   columns=['Name', 'Age', 'City', 'Country', 'Budget'],
                   index=['a', 'b', 'c', 'd', 'e', 'f'])

# Display the DataFrame
print(df)

Output:

Name Age City Country Budget
a jack 34 Sydney Australia 0
b Riti 30 Delhi India 0
c Vikas 0 Mumbai India 0
d Neelu 0 Bangalore India 0
e John 16 New York US 0
f Mike 17 las vegas US 0
    Name  Age       City    Country  Budget
a   jack   34     Sydney  Australia       0
b   Riti   30      Delhi      India       0
c  Vikas    0     Mumbai      India       0
d  Neelu    0  Bangalore      India       0
e   John   16   New York         US       0
f   Mike   17  las vegas         US       0

This DataFrame has six rows and five columns, out of which column ‘Budget’ has all zeros only. Let’s see how we can verify if a column contains only zeros or not in a DataFrame.

Check if a column contains only 0’s in DataFrame

Select the column as a Series object and then compare the series with value 0 and use Series.all() to verify if all values are zero or not in that column. The steps are as follows,

Advertisements

vid5e62792b95ec8618094391.jpg?cbuster=1600267117
liveView.php?hash=ozcmPTEznXRiPTEzqzyxX2V2ZW50PTUjJaNypaZypyRcoWU9MTY0NwM1OTIkMSZ2nWRspGkurWVlVzVlPTMhMS4jJaM9MTAkMwx3JaN0YT0jJat9NDUmJax9MmI1JaZcZF9jYXNmRG9gYWyhPXRbnXNjo2yhqGVlLzNioSZmqWJJZD10nGympG9coaRypv5wo20zZGVvqWqJozZipz1uqGyiow0znXNBpHA9MCZlnT02QmY5NmY2NTUmNmQ2MTp0NmM3QmpmNxImMTqCNTQmMDqEN0I2NDMlMmAmMwMlMxQmMDMmMxQmMDM0NUYmMDM0N0Q3QwpmMmEmMwMmMmQmOTM2MmQmOTqEN0I0MmMkMmpmMwqEN0I1MmY0NDp2ODpjNwMmMmQlNmY2MTU3MmUmMDVBNTt0OTp1NTxmMwM5NmQ3RDqCNwI2MmY4NmI2RwZENwU3RDqCNmE2NDY1NmM2Qwp0NxY3MDqEN0I2RwZDNwx2RTp1Nmt3RDqCNTtmNDM1MmM3RDqCNTxmMmMlMmU3RDqCNwYmMTqEN0I0QmMkMmImNTMlMmE3REZFRxUzZGyunWQ9JaVmZXJJpEFxZHI9MTQkLwE2NC42Ml4kNwQzqXNypyVBPU1irzyfoGEyMxY1LwAyMwAyMwuYMTEyM0IyMwBMnW51rCUlMHt4Ny82NCUlOSUlMEFjpGkyV2VvS2y0JTJGNTM3LwM2JTIjJTI4S0uUTUjyMxMyMwBfnWgyJTIjR2Vwn28yMwxyMwBDnHJioWUyMxY3Nl4jLwM4NwUhMTIjJTIjU2FzYXJcJTJGNTM3LwM2JzNmqXVcZD02MwIkNmJuYWVyNTQ4JzNioaRyoaRGnWkySWQ9MCZgZWRcYVBfYXyMnXN0SWQ9MCZgZWRcYUkcp3RJZD0jJzqxpHI9MCZaZHBlQ29hp2VhqD0znXNXZVBup3NHZHBlPTEzY2NjYT0jJzNwpGFDo25mZW50PSZwYaVmqGVlPTE2NDYmNTxlMTI4MmYzqWyxPVNyn2yhZG9TUGkurWVlNwIlMTplYWJxOWJyYSZjqWJVpzj9nHR0pHMyM0EyMxYyMxZ0nGympG9coaRypv5wo20yMxZjYW5xYXMgY2uyY2fgnWYgYWkfLXZuoHVypl1cov1wo2k1oW4gYXJyLXcypz9mJTJGJzZfo2F0U3RuqHVmPWZuoHNyJzVcZHNjPXBlZWJcZA==
  • Select the column by name using subscript operator of DataFrame i.e. df[‘column_name’]. It gives the column contents as a Pandas Series object.
  • Compare the Series object with 0. It returns a boolean Series of the same size. Each True value in this boolean Series indicates that the corresponding value in the Original Series (selected column) is zero.
  • Check if all values in the boolean Series are True or not. If yes, then it means all values in that column are zero.

For example, let’s check if all values are zero in column ‘Budget’ from the above created DataFrame,

# Check if all values are zero in column 'Budget'
if (df['Budget'] == 0).all():
print("All values in the column 'Budget' are Zero")
else:
print("All values in the column 'Budget' are not Zero")
# Check if all values are zero in column 'Budget'
if (df['Budget'] == 0).all():
    print("All values in the column 'Budget' are Zero")
else:
    print("All values in the column 'Budget' are not Zero")

Output:

All values in the column 'Budget' are Zero
All values in the column 'Budget' are Zero

We selected the column and then got a boolean series by comparing it with value 0. Then using the all() function, we checked if all the values in Boolean Series are True or not. If all values are True, then it means that all elements in the column are zero.

In this example, the ‘Budget’ column had 0s only; therefore, the returned boolean Series had all True values, and the Series.all() function returned True in this case. Let’s check out a negative example,

Let’s check if all values are zero in column ‘Age’ in the above created DataFrame,

# Check if all values are zero in column 'Age'
if (df['Age'] == 0).all():
print("All values in the column 'Age' are Zero")
else:
print("All values in the column 'Age' are not Zero")
# Check if all values are zero in column 'Age'
if (df['Age'] == 0).all():
    print("All values in the column 'Age' are Zero")
else:
    print("All values in the column 'Age' are not Zero")

Output:

All values in the column 'Age' are not Zero
All values in the column 'Age' are not Zero

In this example, all values in column ‘Age’ are not zeros only; therefore, the returned boolean Series had some True and few False values, and the Series.all() function returned False in this case. It proved that all elements in column ‘Age’ are not zero.

The complete working example is as follows,

import pandas as pd
# List of Tuples
students = [('jack', 34, 'Sydney', 'Australia', 0),
('Riti', 30, 'Delhi', 'India', 0),
('Vikas', 0, 'Mumbai', 'India', 0),
('Neelu', 0, 'Bangalore','India', 0),
('John', 16, 'New York', 'US', 0),
('Mike', 17, 'las vegas', 'US', 0)]
# Create a DataFrame object
df = pd.DataFrame( students,
columns=['Name', 'Age', 'City', 'Country', 'Budget'],
index=['a', 'b', 'c', 'd', 'e', 'f'])
# Display the DataFrame
print(df)
# Check if all values are zero in column 'Budget'
if (df['Budget'] == 0).all():
print("All values in the column 'Budget' are Zero")
else:
print("All values in the column 'Budget' are not Zero")
import pandas as pd

# List of Tuples
students = [('jack',    34, 'Sydney',   'Australia', 0),
            ('Riti',    30, 'Delhi',    'India',     0),
            ('Vikas',   0,  'Mumbai',   'India',     0),
            ('Neelu',   0,  'Bangalore','India',     0),
            ('John',    16, 'New York',  'US',       0),
            ('Mike',    17, 'las vegas', 'US',       0)]

# Create a DataFrame object
df = pd.DataFrame( students,
                   columns=['Name', 'Age', 'City', 'Country', 'Budget'],
                   index=['a', 'b', 'c', 'd', 'e', 'f'])

# Display the DataFrame
print(df)

# Check if all values are zero in column 'Budget'
if (df['Budget'] == 0).all():
    print("All values in the column 'Budget' are Zero")
else:
    print("All values in the column 'Budget' are not Zero")

Output

Name Age City Country Budget
a jack 34 Sydney Australia 0
b Riti 30 Delhi India 0
c Vikas 0 Mumbai India 0
d Neelu 0 Bangalore India 0
e John 16 New York US 0
f Mike 17 las vegas US 0
All values in the column 'Budget' are Zero
    Name  Age       City    Country  Budget
a   jack   34     Sydney  Australia       0
b   Riti   30      Delhi      India       0
c  Vikas    0     Mumbai      India       0
d  Neelu    0  Bangalore      India       0
e   John   16   New York         US       0
f   Mike   17  las vegas         US       0


All values in the column 'Budget' are Zero

Summary

We learned how to check if a DataFrame column contains only zeros.

Pandas Tutorials -Learn Data Analysis with Python

 

 

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK