Apply function to every row in a Pandas DataFrame in Python


In this tutorial, we are going to learn about the most common methods of a list i.e.., append() and extend(). Let's see them one by one.

apply()

It is used to apply a function to every row of a DataFrame. For example, if we want to multiply all the numbers from each and add it as a new column, then apply() method is beneficial. Let's see different ways to achieve it.

Example

# importing the pandas package
import pandas as pd
# function to multiply
def multiply(x, y):
   return x * y
# creating a dictionary for DataFrame
data = {
   'Maths': [10, 34, 53],
   'Programming': [23, 12, 43]
}
# creating DataFrame using the data
data_frame = pd.DataFrame(data)
# displaying DataFrame
print('--------------------Before------------------')
print(data_frame)
print()
# applying the function multiply
data_frame['Multiply'] = data_frame.apply(lambda row : multiply(row['Maths'], row['
Programming']), axis = 1)
# displaying DataFrame
print('--------------------After------------------')
print(data_frame)

Output

If you run the above program, you will get the following results.

--------------------Before------------------
Maths Programming
0 10 23
1 34 12
2 53 43
--------------------After------------------
Maths Programming Multiply
0 10 23 230
1 34 12 408
2 53 43 2279

Example

We can also use predefined functions like sum, pow, etc..,

# importing the pandas package
import pandas as pd
# creating a dictionary for DataFrame
data = {
   'Maths': [10, 34, 53],
   'Programming': [23, 12, 43]
}
# creating DataFrame using the data
data_frame = pd.DataFrame(data)
# displaying DataFrame
print('--------------------Before------------------')
print(data_frame)
print()
# applying the function multiply
# using built-in sum function
data_frame['Multiply'] = data_frame.apply(sum, axis = 1)
# displaying DataFrame
print('--------------------After------------------')
print(data_frame)

Output

If you run the above program, you will get the following results.

--------------------Before------------------
Maths Programming
0 10 23
1 34 12
2 53 43
--------------------After------------------
Maths Programming Multiply
0 10 23 33
1 34 12 46
2 53 43 96

Example

We can also use functions from the numpy module. Let's see one example.

# importing the pandas package
import pandas as pd
# importing numpy module for functions
import numpy as np
# creating a dictionary for DataFrame
data = {
   'Maths': [10, 34, 53],
   'Programming': [23, 12, 43]
}
# creating DataFrame using the data
data_frame = pd.DataFrame(data)
# displaying DataFrame
print('--------------------Before------------------')
print(data_frame)
print()
# applying the function multiply
# using sum function from the numpy module
data_frame['Multiply'] = data_frame.apply(np.sum, axis = 1)
# displaying DataFrame
print('--------------------After------------------')
print(data_frame)

Output

If you run the above program, you will get the following results.

--------------------Before------------------
Maths Programming
0 10 23
1 34 12
2 53 43
--------------------After------------------
Maths Programming Multiply
0 10 23 33
1 34 12 46
2 53 43 96

Conclusion

In the above ways, we can use apply() method of DataFrame to apply a function for all the rows. If you have any doubts regarding the tutorial, mention them in the comment section.

raja
Published on 01-Nov-2019 09:50:20
Advertisements