Apply function takes a function as an argument and execute the function in all the elements of the dataframe.

For example, if we want to create a new column which is the square root of another column’s values or apply a complex function and combine one or more columns or when creating new features using the existing features for feature engineering.

Syntax: df.apply(func, axis=0, broadcast=None, raw=False, reduce=None, result_type=None, args=(), **kwds)

df – a pandas series.

func – A lambda function or a normal function

axis – 0 – rows which is default, 1 – columns

1 2 3 |
import pandas as pd import numpy as np |

Lets try this using an example.

1 2 3 4 5 6 |
fruit = { 'orange' : [3,2,0,1], 'apples' : [0,3,7,2] } df = pd.DataFrame(fruit) df |

Output:

orange | apples | |
---|---|---|

0 | 3 | 0 |

1 | 2 | 3 |

2 | 0 | 7 |

3 | 1 | 2 |

For example, we need to create a new series by taking square root of another column. The below code does this.

1 2 3 4 5 6 7 8 |
lst= [] print("Original values in Orange column:") print(df['orange']) for i in df['orange']: lst.append(np.sqrt(i)) print("Square root value if Orange column:") print(lst) |

Output:

1 2 3 4 5 6 7 8 9 |
Original values in Orange column: 0 3 1 2 2 0 3 1 Name: orange, dtype: int64 Square root value if Orange column: [1.7320508075688772, 1.4142135623730951, 0.0, 1.0] |

### Apply function simplifies this.

1 2 |
df['orange_sqrt'] = df['orange'].apply(np.sqrt) df |

Output:

orange | apples | orange_sqrt | |
---|---|---|---|

0 | 3 | 0 | 1.732051 |

1 | 2 | 3 | 1.414214 |

2 | 0 | 7 | 0.000000 |

3 | 1 | 2 | 1.000000 |

We have done the same functionality with less code.

### Using Lambda function:

1 2 |
df['orange_sq'] = df['orange'].apply(lambda x: x*x) df |

Out[25]:

orange | apples | orange_sqrt | orange_sq | |
---|---|---|---|---|

0 | 3 | 0 | 1.732051 | 9 |

1 | 2 | 3 | 1.414214 | 4 |

2 | 0 | 7 | 0.000000 | 0 |

3 | 1 | 2 | 1.000000 | 1 |

Today we learned about Apply function.

Hope you are excited to practice what we have learned now.

We will meet with a new tip in Python. Thank you! 👍

Like to support? Just click the heart icon ❤️.

Happy Programming!🎈