Pandas - DataFrame reindex function returns a warning
代码有什么问题?:
它返回一个警告:
Warning (from warnings module): File
"C:\Python27\lib\site-packages
umpy\core
umeric.py", line 2515
return bool(asarray(a1 == a2).all()) FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will
perform elementwise comparison
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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(' ') 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 umpy\core umeric.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 |
试试这个:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | Frame4 =pd.DataFrame(Data1) print(' ') print Frame4 Frame4.index = ['a','b','c','d','e'] print Frame4 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 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 |
在尝试更改索引轴的名称时,必须使用
1 2 3 4 5 6 7 8 | 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 |
应用
默认情况下,新索引中没有数据框中相应记录的值将被指定为
因此,当您将新索引逻辑指定为