Python | Pandas Series / Dataframe.any ()

NumPy | Python Methods and Functions

Pandas any() method is applicable to both series and Dataframe. It checks to see if any -or the value in the caller object (Dataframe or series) is not 0, and returns True for that. If all values ​​are 0, False is returned.

Syntax: DataFrame.any (axis = 0, bool_only = None, skipna = True, level = None, ** kwargs)

Parameters:
axis: 0 or `index` to apply method by rows and 1 or `columns` to apply by columns.
bool_only: Checks for bool only series in Data frame, if none found, it will use only boolean values. This parameter is not for series since there is only one column.
skipna: Boolean value, If False, returns True for whole NaN column / row
level: int or str, specifies level in case of multilevel

Return type: Boolean series

Example # 1: index implementation

This example creates a sample data frame by passing a dictionary to the Pandas DataFrame () method. Null values ​​are also passed to some indices using Numpy np.nan to test null-valued behavior. Because this example implements the method for the index, the axis parameter remains at 0 (which means rows).

# pandas module import

import pandas as pd 

  
# numpy module import

import numpy as np

 
# create a dictionary

dic = { ` A` : [ 1 , 2 , 3 , 4 , 0 , np.nan, 3 ],

`B` : [ 3 , 1 , 4 , 5 , 0 , np.nan, 5 ],

`C` : [ 0 , 0 , 0 , 0 , 0 , 0 , 0 ]}

 
# create a frame using a dictionary

data = pd.DataFrame (dic)

 
# call the data.any column

result = data. any (axis = 0 )

 
# displaying the result
result

Output:
As shown in the output, since the last column all values ​​are zero, hence False was returned only for this column. 

Example # 2: column-wise

This example creates a sample data frame by passing a dictionary to the Pandas DataFrame () method as in the example above. But instead of passing 0 to the axis parameter, 1 is passed to implement for each value in each column.

# pandas module import

import pandas as pd 

 
# numpy module import

import numpy as np

 
# create a dictionary

dic = { `A` : [ 1 , 2 , 3 , 4 , 0 , np.nan, 3 ],

`B` : [ 3 , 1 , 4 , 5 , 0 , np.nan, 5 ],

`C` : [ 0 , 0 , 0 , 0 , 0 , 0 , 0 ]}

 
# create a frame using a dictionary

data = pd.DataFrame (dic)

 
# call the data.any column

result = data. any (axis = 1 )

 
# displaying the result
result

Output:
As shown in the output, False was returned only for stro to where all values ​​were 0 or NaN and 0.





Get Solution for free from DataCamp guru