Skip to content Skip to sidebar Skip to footer

Efficiently Processing Dataframe Rows With A Python Function?

In many places in our Pandas-using code, we have some Python function process(row). That function is used over DataFrame.iterrows(), taking each row, and doing some processing, and

Solution 1:

You should apply your function along the axis=1. Function will receive a row as an argument, and anything it returns will be collected into a new series object

df.apply(you_function, axis=1)

Example:

>>> df = pd.DataFrame({'a': np.arange(3),
                       'b': np.random.rand(3)})
>>> df
   a         b
000.880075110.143038220.795188>>> deffunc(row):
        return row['a'] + row['b']
>>> df.apply(func, axis=1)
00.88007511.14303822.795188
dtype: float64

As for the second part of the question: row wise operations, even optimised ones, using pandas apply, are not the fastest solution there is. They are certainly a lot faster than a python for loop, but not the fastest. You can test that by timing operations and you'll see the difference.

Some operation could be converted to column oriented ones (one in my example could be easily converted to just df['a'] + df['b']), but others cannot. Especially if you have a lot of branching, special cases or other logic that should be perform on your row. In that case, if the apply is too slow for you, I would suggest "Cython-izing" your code. Cython plays really nicely with the NumPy C api and will give you the maximal speed you can achieve.

Or you can try numba. :)

Post a Comment for "Efficiently Processing Dataframe Rows With A Python Function?"