代码有什么问题?: 它返回了一个警告:
警告(来自警告模块):文件 " C:\ Python27 \ lib \ site-packages \ numpy \ core \ numeric.py",第2515行 return bool(asarray(a1 == a2).all())FutureWarning:elementwise比较失败;返回标量,但将来会 执行元素比较
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
答案 0 :(得分:0)
在尝试更改索引轴的名称时,必须使用rename
代替reindex
。
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
应用reindex
的目的是在新索引选择逻辑中对齐数据帧的索引。
默认情况下,新索引中数据框中没有相应记录的值将被指定为NaN
。
因此,当您将新索引逻辑指定为list('abcde')
时,它会检查所有索引值,但由于之前的索引位于range(0,4)
,因此无法找到匹配项。因此,它返回了Nans
。
答案 1 :(得分:0)
试试这个:
Frame4 =pd.DataFrame(Data1)
print('\n')
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