# When should/shouldn't we use pandas apply() in our code?

Learn and differentiate the Python pandas code with and without apply() method.
Submitted by Pranit Sharma, on June 15, 2022

You must have noticed many times programmers suggest avoiding the code that involves the use of the Pandas method apply(). You might have also seen users sharing their problems after using the apply() function as it is slow.

Considering the above concern concludes with two major questions:

1. How and when should we make our code male apply() free?
2. Are there any situations when using apply() is beneficial?

Here, we are going to learn the solution to the above two questions. How and when should we make our code male apply() free?

The answer to this question is easy. Whenever we are working on some numeric data, always have multiple options to operate. For example, we can add two numbers without even using the '+' operator. Or in the case of DataFrame, if we want to calculate the sum of the column values, we do not need to use apply() method, instead, we can directly use the sum() method.

Observe the following example:

To work with pandas, we need to import pandas package first, below is the syntax:

```import pandas as pd
```

With apply() Method:

```# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a dictionary
d = {
'A':[35,54,44,45,33,22],
'B':[928,28,23,2343,232,22],
'C':[221,24,22,20,332,22]
}

# Creating a DataFrame
df = pd.DataFrame(d)

# Display DataFrame
print("Created DataFrame:\n",df,"\n")

# Get the sum of column B
Total = df['B'].apply(np.sum)

# Display total
print("Total sum:\n",Total)
```

Output:

```Created DataFrame:
A     B    C
0  35   928  221
1  54    28   24
2  44    23   22
3  45  2343   20
4  33   232  332
5  22    22   22

Total sum:
0     928
1      28
2      23
3    2343
4     232
5      22
Name: B, dtype: int64
```

Without apply() Method:

```# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a dictionary
d = {
'A':[35,54,44,45,33,22],
'B':[928,28,23,2343,232,22],
'C':[221,24,22,20,332,22]
}

# Creating a DataFrame
df = pd.DataFrame(d)

# Display DataFrame
print("Created DataFrame:\n",df,"\n")

# Get the sum of column B
Total = df['B'].sum()

# Display total
print("Total sum:\n",Total)
```

Output:

```Created DataFrame:
A     B    C
0  35   928  221
1  54    28   24
2  44    23   22
3  45  2343   20
4  33   232  332
5  22    22   22

Total sum:
3576
```

The result of both codes will be similar, but the code where apply() method is not used is much faster.

Are there any situations when using apply() is beneficial?

While working on a DataFrame, we sometimes wanted to perform some operations on a particular row or a particular column, if we want to perform some string operation on some columns, or if we want to perform date or time operations on some columns, we use apply() method.

Analysis where calling a function, again and again, is not an issue for the developers and which leads to effective analysis, the use of apply() is suitable.

Conclusively, we can say that, while dealing with numeric data or when we have other cythonized functions we do not use apply() method but when the functions are limited to a series, not to the entire DataFrame we can take the help of apply() method.

Preparation

What's New

Top Interview Coding Problems/Challenges!