Alignment of DataFrames

The union of two DataFrames occurs based on row and column indices. Let's understand this through an example. Consider two DataFrames:

ore1DF=pd.DataFrame(np.array([[20,35,25,20],
[11,28,32,29]]),
columns=['iron','magnesium',
'copper','silver'])
ore2DF=pd.DataFrame(np.array([[14,34,26,26],
[33,19,25,23]]),
columns=['iron','magnesium',
'gold','silver'])

The + operator will add values in columns with the same labels in both DataFrames:

ore1DF + ore2DF

The following is the output:

The columns—copper and gold—were not found in both the DataFrames. Hence, NA has been appended in these columns.

If you combine a DataFrame object and a Series object, the default behavior is to broadcast the Series object across the rows:

ore1DF + pd.Series([25,25,25,25], index=['iron', 'magnesium', 'copper', 'silver'])

The following is the output:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.113.199