Examples
DataFrame.reindex supports two calling conventions
import numpy as np
import pandas as pd
index = ['Firefox', 'Chrome', 'Safari', 'IE10']
df = pd.DataFrame({
'http_status': [200,200,404,404],
'response_time': [0.04, 0.02, 0.07, 0.08]},
index=index)
df
A new index and reindex the dataframe:
new_index= ['Safari', 'Iceweasel', 'Comodo Dragon','Chrome']
df.reindex(new_index)
You can fill in the missing values by passing a value to the keyword fill_value.
Because the index is not monotonically increasing or decreasing, we cannot use
arguments to the keyword method to fill the NaN values.
df.reindex(new_index, fill_value=0)
df.reindex(new_index, fill_value='missing')
We can also reindex the columns.
df.reindex(columns=['http_status', 'user_agent'])
Or we can use “axis-style” keyword arguments
df.reindex(['http_status', 'user_agent'], axis="columns")
To further illustrate the filling functionality in reindex, we will create a dataframe
with a monotonically increasing index (for example, a sequence of dates).
date_index = pd.date_range('1/1/2019', periods=6, freq='D')
df2 = pd.DataFrame({"prices": [200, 201, np.nan, 200, 190, 188]},
index=date_index)
df2
Suppose we decide to expand the dataframe to cover a wider date range.
date_index2 = pd.date_range('12/29/2018', periods=10, freq='D')
df2.reindex(date_index2)
For example, to back-propagate the last valid value to fill the NaN values,
pass bfill as an argument to the method keyword.
df2.reindex(date_index2, method='bfill')