Get your own Python server Result Size: 625 x 565
x
 
import pandas as pd
data = {
  "age": [50, 40, 30, 40],
  "qualified": [True, False, False, False]
}
idx = ["Sally", "Mary", "John", "Monica"]
df = pd.DataFrame(data, index=idx)
print("--------BEFORE--------")
print(df)
newidx = ["Robert", "Cindy", "Chloe", "Pete"]
newdf = df.reindex(newidx)
print("--------AFTER--------")
print(newdf)
#Notice that the values are set to NaN, which is the default behaviour when the new index is not the same as the old.
  --------BEFORE--------
          age  qualified
  Sally    50       True
  Mary     40      False
  John     30      False
  Monica   40      False
  --------AFTER--------
          age qualified
  Robert  NaN       NaN
  Cindy   NaN       NaN
  Chloe   NaN       NaN
  Pete    NaN       NaN