This week we have a look at Data Alignment in Python

In [1]:

import numpy as np
import pandas as pd
from pandas import Series, DataFrame

 

In [2]:
#create series
ser1 = Series([0,1,3], index=['A','B','C'])
ser1

 

Out[2]:
A    0
B    1
C    3
dtype: int64
In [3]:
ser2 = Series([3,4,5,6], index=['A','B','C','D'])
ser2

 

Out[3]:
A    3
B    4
C    5
D    6
dtype: int64
In [4]:
#adding two series which have different lenghts
ser1 + ser2

 

Out[4]:
A    3.0
B    5.0
C    8.0
D    NaN
dtype: float64
In [6]:
#lets try and add dataframes
#create dataframe
dframe = DataFrame(np.arange(4).reshape((2,2)), columns=list('AB'), index=['LA','GA'])
dframe

 

Out[6]:
A B
LA 0 1
GA 2 3
In [10]:
#create a second dataframe
dframe2 = DataFrame(np.arange(9).reshape((3,3)), columns=list('ABC'), index=['LA','NYC','GA'])
dframe2

 

Out[10]:
A B C
LA 0 1 2
NYC 3 4 5
GA 6 7 8
In [11]:
#add the dataframes up
dframe + dframe2

 

Out[11]:
A B C
GA 8.0 10.0 NaN
LA 0.0 2.0 NaN
NYC NaN NaN NaN
In [13]:
#add the 2 dataframe in such a way that we will not lose the values to NaNs
dframe.add(dframe2, fill_value=0) # fill NaNs values to zero so the new dataframe can use the any original values of any of the frames

 

Out[13]:
A B C
GA 8.0 10.0 8.0
LA 0.0 2.0 2.0
NYC 3.0 4.0 5.0
In [16]:
#make series out of dataframe
ser3 = dframe2.ix[0]
ser3

 

Out[16]:
A    0
B    1
C    2
Name: LA, dtype: int32
In [17]:
#subtract series from a dframe
dframe2 - ser3

 

Out[17]:
A B C
LA 0 0 0
NYC 3 3 3
GA 6 6 6

This is part of lectures on Learning Python for Data Analysis and Visualization by Jose Portilla on Udemy.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *