In [3]:
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
from numpy.random import randn
In [5]:
ser1 = Series([1,2,3,4], index=['A','B','C','D'])
In [6]:
ser1
Out[6]:
In [7]:
ser2 = ser1.reindex(['A','B','C','D','E','F'])
In [8]:
ser2
Out[8]:
In [12]:
ser2.reindex(['A','B','C','D','E','F', 'G'], fill_value=0)
Out[12]:
In [21]:
ser3 = Series(['USA', 'Canada', 'Germany', 'Venezuela'], index=[0,5,10,15])
ser3
Out[21]:
In [24]:
ranger = range(20)
ranger
Out[24]:
In [25]:
ser3.reindex(ranger, method='ffill')
Out[25]:
In [28]:
df = DataFrame(randn(25).reshape((5,5)), index=['A', 'B', 'D', 'E', 'F'],
columns=['col1','col2','col3','col4','col5'])
df
Out[28]:
In [29]:
# reindex including 'C'
df2 = df.reindex(['A', 'B', 'C', 'D', 'E', 'F'])
df2
Out[29]:
In [30]:
new_columns = ['col1','col2','col3','col4','col5', 'col6']
In [34]:
df2 = df2.reindex(columns=new_columns)
df2
Out[34]:
In [35]:
df
Out[35]:
In [37]:
# reindexing using the 'ix' function
df.ix[['A','B','C','D','E','F'], new_columns]
Out[37]:
In [ ]: