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
#create series
ser1 = Series([0,1,3], index=['A','B','C'])
ser1
ser2 = Series([3,4,5,6], index=['A','B','C','D'])
ser2
#adding two series which have different lenghts
ser1 + ser2
#lets try and add dataframes
#create dataframe
dframe = DataFrame(np.arange(4).reshape((2,2)), columns=list('AB'), index=['LA','GA'])
dframe
#create a second dataframe
dframe2 = DataFrame(np.arange(9).reshape((3,3)), columns=list('ABC'), index=['LA','NYC','GA'])
dframe2
#add the dataframes up
dframe + dframe2
#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
#make series out of dataframe
ser3 = dframe2.ix[0]
ser3
#subtract series from a dframe
dframe2 - ser3
This is part of lectures on Learning Python for Data Analysis and Visualization by Jose Portilla on Udemy.