dtoolkit.accessor.dataframe.equal#

dtoolkit.accessor.dataframe.equal(df: DataFrame, /, other, align: bool = True, axis: Literal[0, 1, 'index', 'columns'] = 0, **kwargs) DataFrame[source]#

Return a boolean DataFrame containing the result of comparing with other.

A sugar syntax for np.equal(df, other, **kwargs).

Parameters:
otherscalar, 1d array-like or 2d array-like

The value(s) to compare with the DataFrame.

  • scalar : compare each element with the scalar value.

  • 1d / 2d array-like : compare each element with the corresponding value.

alignbool, default True

If True, align df and other before comparing. Only works while other is a Series / DataFrame.

axis{0 or ‘index’, 1 or ‘columns’}, default 0

If 0, compare along df’s rows else columns. Works only when other is 1d array-like.

**kwargs

Additional keyword arguments are passed to numpy.equal().

Returns:
DataFrame
Raises:
ValueError

If other is id array-like and its size is not equal to df’s size.

Warns:
UserWarning

If align is True and other is a Series / DataFrame, but its indexes (columns) are not equal to df.

See also

pandas.DataFrame.eq

Compare two Series.

numpy.equal

Return (x1 == x2) element-wise.

dtoolkit.accessor.series.equal

Series version of .equal.

Examples

>>> import dtoolkit
>>> import pandas as pd
>>> df = pd.DataFrame({"a": [1, 2, 3], "b": [3, 2, 1]})
>>> df
   a  b
0  1  3
1  2  2
2  3  1

Like DataFrame.eq method to compare scalar or array-like via ==, DataFrame.equal could also do that.

Compare with a scalar.

>>> df == 1
       a      b
0   True  False
1  False  False
2  False   True
>>> df.equal(1)
       a      b
0   True  False
1  False  False
2  False   True

Compare with a 1d array-like.

But == only can compare along the row. DataFrame.equal can compare along the row or column.

>>> df == [1, 2]
       a      b
0   True  False
1  False   True
2  False  False
>>> df.equal([1, 2], axis=0)  # default compare along the row
       a      b
0   True  False
1  False   True
2  False  False
>>> df.equal([1, 2, 3], axis=1)  # compare along the column
      a      b
0  True  False
1  True   True
2  True  False

Compare with a 2d array-like.

>>> import numpy as np
>>> df == np.array([[1, 1], [2, 2], [3, 3]])
      a      b
0  True  False
1  True   True
2  True  False
>>> df.equal(np.array([[1, 1], [2, 2], [3, 3]]))  # pure list is also okay
      a      b
0  True  False
1  True   True
2  True  False