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
dfandotherbefore comparing. Only works whileotheris a Series / DataFrame.- axis{0 or ‘index’, 1 or ‘columns’}, default 0
If 0, compare along
df’s rows else columns. Works only whenotheris 1d array-like.- **kwargs
Additional keyword arguments are passed to
numpy.equal().
- Returns:
- DataFrame
- Raises:
- ValueError
If
otheris id array-like and its size is not equal todf’s size.
- Warns:
- UserWarning
If
alignis True andotheris a Series / DataFrame, but its indexes (columns) are not equal todf.
See also
pandas.DataFrame.eqCompare two Series.
numpy.equalReturn (x1 == x2) element-wise.
dtoolkit.accessor.series.equalSeries 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.eqmethod to compare scalar or array-like via==,DataFrame.equalcould 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.equalcan 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