Example of how to apply a function to a DataFrame row with pandas in python:
Table of contents
Create a simple dataframe with pandas
Let's start by creating a simple dataframe df:
>>> import pandas as pd>>> import numpy as np>>> data = np.arange(1,13)>>> data = data.reshape(3,4)>>> df = pd.DataFrame(data=data,columns=['a','b','c','d'])>>> dfa b c d0 1 2 3 41 5 6 7 82 9 10 11 12
Apply a function on a given row
So let's try to modify the elements of row 1 for example:
>>> df.iloc[1]a 5b 6c 7d 8
If we want to add 10 to all the elements of line 1 we can simply do like this:
>>> df.iloc[1] = df.iloc[1] + 10>>> dfa b c d0 1 2 3 41 15 16 17 182 9 10 11 12
Another example by multiplying all the elements of line 1 by 2:
>>> df.iloc[1] = df.iloc[1] * 2.0>>> dfa b c d0 1.0 2.0 3.0 4.01 30.0 32.0 34.0 36.02 9.0 10.0 11.0 12.0
To apply a more complicated function like a square root for example, a solution is to use the panda function apply():
>>> df.iloc[1].apply(np.sqrt)a 5.477226b 5.656854c 5.830952d 6.000000Name: 1, dtype: float64
or like this
>>> df.apply(lambda x: np.sqrt(x) if x.name == 1 else x, axis=1)a b c d0 1.000000 2.000000 3.000000 4.01 5.477226 5.656854 5.830952 6.02 9.000000 10.000000 11.000000 12.0
Example by defining its own function:
>>> def myfunc(x):... return x**2 + 2*x + 3...>>>>>> df.iloc[1].apply(myfunc)a 963.0b 1091.0c 1227.0d 1371.0
References
| Links | Site |
|---|---|
| Apply a function to a single column in Dataframe | thispointer.com |
| pandas.DataFrame.apply | pandas doc |
