Basic pd.DataFrame arithmetic
Having now covered basic pd.Series
arithmetic, you will find that the corresponding pd.DataFrame
arithmetic operations are practically identical, with the lone exception being that our algorithms now work in two dimensions instead of just one. In doing so, the pandas API makes it easy to interpret data regardless of its shape, and without requiring users to write loops to interact with data. This helps significantly reduce developer effort and helps you write faster code – a win-win for developers.
How it works
Let’s create a small 3x3 pd.DataFrame
using random numbers:
np.random.seed(42)
df = pd.DataFrame(
np.random.randn(3, 3),
columns=["col1", "col2", "col3"],
index=["row1", "row2", "row3"],
).convert_dtypes(dtype_backend="numpy_nullable")
df
col1 col2 col3
row1 0.496714 -0.138264 0.647689
row2 1.52303...