5

I'm struggling to clearly understand the concept of "element" in Pandas. Already went through the document of Pandas and googled around, and I'm guessing it's some sort of row? What do people mean when they say "apply function elment-wise"? This question came up when I was reading this SO post : How to apply a function to two columns of Pandas dataframe

popover
  • 71
  • 1
  • 4

3 Answers3

4

Pandas is designed for operating vector wise operations i.e. taking entire column and operate some function. This you can term as column wise operation. But in some cases you may need to operate element by element (i.e. element wise operation). This type operation is not very efficient.

Here is an example:

import pandas as pd
df = pd.DataFrame([a for a in range(100)], columns=['mynum'])

column wise operation

%%timeit
df['add1'] = df.mynum +1

222 µs ± 3.31 µs per loop

When operated element wise

%%timeit
df['add1'] = df.apply(lambda a: a.mynum+1, axis = 1)

2.33 ms ± 85.4 µs per loop

Chitrasen
  • 1,706
  • 18
  • 15
4

I believe "element" in Pandas is an inherited concept of the "element" from NumPy. Give the first few paragraphs of the docs on ufuncs a read.

Each universal function takes array inputs and produces array outputs by performing the core function element-wise on the inputs (where an element is generally a scalar, but can be a vector or higher-order sub-array for generalized ufuncs).

In mathematics, element-wise operations refer to operations on individual elements of a matrix.

Examples:

import numpy as np

>>> x, y = np.arange(1,5).reshape(2,2), 3*np.eye(2)
>>> x, y
>>> x, y = np.arange(1,5).reshape(2,2), 3*np.eye(2)
>>> x, y
(array([[1, 2],
        [3, 4]]),
 array([[3., 0.],
        [0., 3.]]))
>>> x + y   # element-wise addition
array([[4., 2.],
       [3., 7.]])
columns of y
>>> np.dot(x,y)    # NOT element-wise multiplication (matrix multiplication)
# elements become dot products of the rows of x with columns of y
array([[ 3.,  6.],
       [ 9., 12.]])
>>> x * y   # element-wise multiplication
array([[ 3.,  0.],
       [ 0., 12.]])

I realize your question was about Pandas, but element-wise in Pandas means the same thing it does in NumPy and in linear algebra (as far as I'm aware).

Unique Divine
  • 151
  • 2
  • 5
2

Element-wise means handling data element by element.

cs95
  • 379,657
  • 97
  • 704
  • 746
Bin
  • 209
  • 1
  • 3
  • 8