Pandas DataFrame and Series Alignment Python Notes
This week we have a look at Data Alignment in Python
In [1]:
|
1 2 3 |
import numpy as np import pandas as pd from pandas import Series, DataFrame |
|
1 2 3 |
#create series ser1 = Series([0,1,3], index=['A','B','C']) ser1 |
|
1 2 |
ser2 = Series([3,4,5,6], index=['A','B','C','D']) ser2 |
|
1 2 |
#adding two series which have different lenghts ser1 + ser2 |
|
1 2 3 4 |
#lets try and add dataframes #create dataframe dframe = DataFrame(np.arange(4).reshape((2,2)), columns=list('AB'), index=['LA','GA']) dframe |
|
1 2 3 |
#create a second dataframe dframe2 = DataFrame(np.arange(9).reshape((3,3)), columns=list('ABC'), index=['LA','NYC','GA']) dframe2 |
|
1 2 |
#add the dataframes up dframe + dframe2 |
|
1 2 |
#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 |
|
1 2 3 |
#make series out of dataframe ser3 = dframe2.ix[0] ser3 |
|
1 2 |
#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.


