# Pandas: Calculate the element-wise sum of 2 DataFrames

Updated: February 19, 2024 By: Guest Contributor Post a comment

## Introduction

When working with data in Python, Pandas is an indispensable library that provides data structures and data analysis tools. In this tutorial, weâ€™ll explore how to calculate the element-wise sum of two DataFrames. This operation is beneficial when handling similar datasets that require aggregation to analyze trends or perform statistical operations. Weâ€™ll start with the basics and gradually move to more sophisticated examples. Whether you are a beginner or a seasoned data analyst, understanding how to perform these calculations efficiently can save you a lot of time.

## Getting Started

First, ensure you have Pandas installed:

``pip install pandas``

Next, import pandas library:

``import pandas as pd``

## Basic Operations

Assuming you have the following two DataFrames:

``````df1 = pd.DataFrame({
'A': [1, 2, 3],
'B': [4, 5, 6]
})

df2 = pd.DataFrame({
'A': [10, 20, 30],
'B': [40, 50, 60]
})``````

Calculating the element-wise sum is as simple as using the `+` operator:

``````sum_df = df1 + df2
print(sum_df)``````

Output:

``````    A   B
0  11  44
1  22  55
2  33  66``````

This basic operation works well when the DataFrames have the same shape and corresponding indexes.

## Handling Different Shapes

If your DataFrames have different shapes, pandas automatically aligns them by index. However, youâ€™d likely encounter `NaN` values where data does not align. To handle this, you might want to use the `add` method with the `fill_value` parameter:

``df1.add(df2, fill_value=0)``

Moving on to more complex scenarios, imagine if the DataFrames are not perfectly aligned either by indexes or columns. In such cases, direct addition wonâ€™t yield the expected results, and using `add` with `fill_value` becomes more relevant. Hereâ€™s an example:

``````df3 = pd.DataFrame({
'A': [10, 20],
'C': [30, 40]
}, index=[1, 2])

sum_df = df1.add(df3, fill_value=0)
print(sum_df)``````

Output:

``````      A     B     C
0   1.0   4.0   NaN
1  22.0   5.0  30.0
2  23.0   6.0  40.0``````

This example shows how to account for differences in both indexes and columns.

## Using `apply` and Custom Functions

In some cases, you might want to apply a custom function to perform the addition. This is particularly useful when needing more control over the calculation or when dealing with non-numeric data that requires a specific handling. You can use the `apply` method along with a lambda function:

``sum_df = df1.apply(lambda x: x + df2)``

## Conclusion

Through this tutorial, weâ€™ve seen how to calculate the element-wise sum of two DataFrames, starting from simple direct additions to handling more complex cases with different shapes or using custom functions for more control. Having a strong understanding of these operations is crucial for efficient data manipulation and analysis in Pandas. As you work with more datasets, experimenting with these techniques will help you find the best approach for your specific needs.

Search tutorials, examples, and resources