Overview
- The Python library pandas, offers several methods to handle two-dimensional data through the class DataFrame.
- Two DataFrames can be added by using the add() method of pandas DataFrame class.
- Calling add() method is similar to calling the operator +. However, the add() method can be passed a fill value, which will be used for NaN values in the DataFrame.
Example
import pandas as pd
dataSet1 = [(10, 20, 30),
(40, 50, 60),
(70, 80, 90)];
dataFrame1 = pd.DataFrame(data=dataSet1)
dataSet2 = [(5, 15, 25),
(35, 45, 55),
(65, 75, 85)];
dataFrame2 = pd.DataFrame(data=dataSet2)
print("DataFrame1:");
print(dataFrame1);
print("DataFrame2:");
print(dataFrame2);
result = dataFrame1.add(dataFrame2);
print("Result of adding two pandas dataframes:");
print(result)Output
DataFrame1:
0 1 2
0 10 20 30
1 40 50 60
2 70 80 90
DataFrame2:
0 1 2
0 5 15 25
1 35 45 55
2 65 75 85
Result of adding two pandas dataframes:
0 1 2
0 15 35 55
1 75 95 115
2 135 155 175##