Shank Shank - 4 months ago 34
Python Question

Pandas - DataFrame reindex function returns a warning

Whats wrong with the code?:
Its returning a warning :


Warning (from warnings module): File
"C:\Python27\lib\site-packages\numpy\core\numeric.py", line 2515
return bool(asarray(a1 == a2).all()) FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will
perform elementwise comparison


import pandas as pd
import numpy as np

Data1 = {'State':['Ohio','Ohio','Ohio','Nevada','Nevada'],'Year':[2000,2001,2002,2001,2002],'POP':[1.5,1.7,3.6,2.4,2.9]}

Frame4 =pd.DataFrame(Data1)
print('\n')
print Frame4

Frame5 = Frame4.reindex(['a','b','c','d','e'])
print Frame5

my o/p
POP State Year
0 1.5 Ohio 2000
1 1.7 Ohio 2001
2 3.6 Ohio 2002
3 2.4 Nevada 2001
4 2.9 Nevada 2002

Warning (from warnings module):
File "C:\Python27\lib\site-packages\numpy\core\numeric.py", line 2515
return bool(asarray(a1 == a2).all())
FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
POP State Year
a NaN NaN NaN
b NaN NaN NaN
c NaN NaN NaN
d NaN NaN NaN
e NaN NaN NaN

Answer

You must use rename instead of reindex as you are trying to change the names of the index axis.

Frame5 = Frame4.rename({0:'a', 1:'b', 2:'c', 3:'d', 4:'e'})
print(Frame5)
   POP   State  Year
a  1.5    Ohio  2000
b  1.7    Ohio  2001
c  3.6    Ohio  2002
d  2.4  Nevada  2001
e  2.9  Nevada  2002

The purpose of applying reindex is to align the indices of the dataframe in the new index selection logic.

By default, values in the new index that do not have corresponding records in the dataframe are assigned NaN.

Hence, when you specified the new index logic as list('abcde'), it checked all the index values but couldn't find a match as the prior indices had been in range(0,4). So, it returned Nansinstead.

Comments