Skip to content

Instantly share code, notes, and snippets.

@hiropppe
Last active February 7, 2018 21:53
Show Gist options
  • Save hiropppe/95929631c1dcf5f2fa2d9778667d1d77 to your computer and use it in GitHub Desktop.
Save hiropppe/95929631c1dcf5f2fa2d9778667d1d77 to your computer and use it in GitHub Desktop.
Pandasで各行に任意の関数を適用するサンプル。遅いらしい:-)
In [1]: import pandas as pd
In [2]: data = {'a': [0,1,2], 'b': [3,4,5]}
...: df = pd.DataFrame(data)
...: df
...:
Out[2]:
a b
0 0 3
1 1 4
2 2 5
In [3]: def f(r):
...: return '{:d} + {:d} = {:d}'.format(r['a'], r['b'], r['a'] + r['b'])
...:
In [4]: df['c'] = df.apply(f, axis=1)
...: df
...:
Out[4]:
a b c
0 0 3 0 + 3 = 3
1 1 4 1 + 4 = 5
2 2 5 2 + 5 = 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment