w3resource

Pandas: Series - all() function

Test whether all element is true over requested Pandas axis

The all() function is used to check whether all elements are True, potentially over an axis.

Returns True unless there at least one element within a series or along a Dataframe axis that is False or equivalent (e.g. zero or empty).

Syntax:

Series.all(self, axis=0, bool_only=None, skipna=True, level=None, **kwargs)
Pandas Series all image

Parameters:

Name Description Type/Default Value Required / Optional
axis Indicate which axis or axes should be reduced.
  • 0 / ‘index’ : reduce the index, return a Series whose index is the original column labels.
  • 1 / ‘columns’ : reduce the columns, return a Series whose index is the original index.
  • None : reduce all axes, return a scalar.
 

{0 or ‘index’, 1 or ‘columns’, None}
Default Value: 0

Required
bool_only Include only boolean columns. If None, will attempt to use everything, then use only boolean data. Not implemented for Series. bool
Default Value: None
Required
skipna Exclude NA/null values. If the entire row/column is NA and skipna is True, then the result will be True, as for an empty row/column. If skipna is False, then NA are treated as True, because these are not equal to zero. bool
Default Value: True
Required
level If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a scalar. nt or level name
Default Value: None
Required
kwargs Additional keywords have no effect but might be accepted for compatibility with NumPy. any
Default Value: None
Required

Returns: scalar or Series
If level is specified, then, Series is returned; otherwise, scalar is returned.

Example - Series:

Python-Pandas Code:

import numpy as np
import pandas as pd
pd.Series([True, True]).all()

Output:

True

Python-Pandas Code:

import numpy as np
import pandas as pd
pd.Series([False, True]).all()

Output:

False

Python-Pandas Code:

import numpy as np
import pandas as pd
pd.Series([]).all()

Output:

True

Python-Pandas Code:

import numpy as np
import pandas as pd
pd.Series([np.nan]).all()

Output:

True

Python-Pandas Code:

import numpy as np
import pandas as pd
pd.Series([np.nan]).all(skipna=False)

Output:

True

Example - DataFrames:

Create a dataframe from a dictionary.

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({'c1': [True, True], 'c2': [True, False]})
df

Output:

    c1	c2
0	True	True
1	True	False

Example - Default behaviour checks if column-wise values all return True:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({'c1': [True, True], 'c2': [True, False]})
df.all()

Output:

c1     True
c2    False
dtype: bool

Example - Specify axis='columns' to check if row-wise values all return True:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({'c1': [True, True], 'c2': [True, False]})
df.all(axis='columns')

Output:

0     True
1    False
dtype: bool

Example - Or axis=None for whether every value is True:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({'c1': [True, True], 'c2': [True, False]})
df.all(axis=None)

Output:

False

Previous: Pandas absolute value of column
Next: Test whether any element is true over requested Pandas axis



Follow us on Facebook and Twitter for latest update.