import pandas as pd
from functools import reduce

Map in bare Python

a = [1,2,3,4,5]
list(map(lambda x: x**2, a))
[1, 4, 9, 16, 25]
list(filter(lambda x: x >= 4, a))
[4, 5]
reduce(lambda x,y: x*y, a)
120
b = range(1000)
%timeit list(map(lambda x: x**2, b))
1000 loops, best of 3: 579 µs per loop
%timeit [x**2 for x in b]
1000 loops, best of 3: 468 µs per loop

Map in Pandas

keys = [l for l in 'abcdefghei']
keys
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'e', 'i']
val = [v for v, _  in enumerate(keys)]
val
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
df = pd.DataFrame({'keys': keys, 'values': val})
df
keys values
0 a 0
1 b 1
2 c 2
3 d 3
4 e 4
5 f 5
6 g 6
7 h 7
8 e 8
9 i 9
Apply function element-wise to the whole DataFrame: the function is applied on an element-by-element basis !!!
df.applymap(str)
keys values
0 a 0
1 b 1
2 c 2
3 d 3
4 e 4
5 f 5
6 g 6
7 h 7
8 e 8
9 i 9
Apply function to DataFrame column-wise: function is applied to a column as a whole !!!
df.apply(lambda x: min(x), axis=0)
keys      a
values    0
dtype: object
Apply function to DataFrame row-wise: function is applied to a row as a whole !!!
df.apply(lambda x: ' '.join([str(el) for el in x]), axis=1)
0    a 0
1    b 1
2    c 2
3    d 3
4    e 4
5    f 5
6    g 6
7    h 7
8    e 8
9    i 9
dtype: object
df.apply?
Apply function to a DataSeries element-wise
df['values'].map(lambda x: x*10)
0     0
1    10
2    20
3    30
4    40
5    50
6    60
7    70
8    80
9    90
Name: values, dtype: int64

In summary, for Pandas:

  • to apply function to the whole DataFrame element-wise: df.applymap(func)
  • to apply function column- or row-wise: df.apply(func, axis)
  • to apply function to a DataSeries element-wise: df['name'].map(func)
Write a comment:

*

Your email address will not be published.

© 2014 In R we trust.
Top
Follow us: