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 |
In [2]:
|
1 2 3 |
#create series ser1 = Series([0,1,3], index=['A','B','C']) ser1 |
Out[2]:
|
1 2 3 4 |
A 0 B 1 C 3 dtype: int64 |
In [3]:
|
1 2 |
ser2 = Series([3,4,5,6], index=['A','B','C','D']) ser2 |
Out[3]:
|
1 2 3 4 5 |
A 3 B 4 C 5 D 6 dtype: int64 |
In [4]:
|
1 2 |
#adding two series which have different lenghts ser1 + ser2 |
Out[4]:
|
1 2 3 4 5 |
A 3.0 B 5.0 C 8.0 D NaN dtype: float64 |
In [6]:
|
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 |
Out[6]: A B LA 0 1 GA 2 3 In [10]:
|
1 2 3 |
#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…
