Change language

Python | Pandas Series / Dataframe.any ()

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.

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically