While using reindex method on any dataframe why do original values go missing?

This is the original Dataframe: What I wanted : I wanted to convert this above data-frame into this multi-indexed column data-frame : I managed to do it by this piece of code :

# tols : original dataframe
cols = pd.MultiIndex.from_product([['A','B'],['Y','X'] 
['P','Q']])
tols.set_axis(cols, axis = 1, inplace = False)

What I tried : I tried to do this with the reindex method like this :

cols = pd.MultiIndex.from_product([['A','B'],['Y','X'], 
['P','Q']])
tols.reindex(cols, axis = 'columns')

it resulted in an output like this :

My problem : As you could see in the output above all my original numerical values go missing on employing the reindex method. In the documentation page it was clearly mentioned : Conform DataFrame to new index with optional filling logic, placing NA/NaN in locations having no value in the previous index. A new object is produced unless the new index is equivalent to the current one. So i don't understand:

  • Where did i particularly err in employing the reindex method to lose my original values
  • How should i have employed the reindex method correctly to get my desired output

Topic dataframe pandas data-indexing-techniques python

Category Data Science


Regarding your code the tols must be cols?

cols = pd.MultiIndex.from_product([['A','B'],['Y','X'] 
['P','Q']])
tols.set_axis(cols, axis = 1, inplace = False)

This must be:

cols = pd.MultiIndex.from_product([['A','B'],['Y','X'] 
['P','Q']])
cols.set_axis(cols, axis = 1, inplace = False)

Personally I would have removed nan with a default value before performing the multi-indexing.

df_noNAN = from_product.fillna(value=-1)

In your case it looks like the inplace is producing a bug and better reported to the pandas team. On Stack exchange they are extremely responsive.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.