我将pandas导入为pd并运行下面的代码并获得以下结果
码:
traindataset = pd.read_csv('/Users/train.csv')
print traindataset.dtypes
print traindataset.shape
print traindataset.iloc[25,3]
traindataset.dropna(how='any')
print traindataset.iloc[25,3]
print traindataset.shape
产量
TripType int64
VisitNumber int64
Weekday object
Upc float64
ScanCount int64
DepartmentDescription object
FinelineNumber float64
dtype: object
(647054, 7)
nan
nan
(647054, 7)
[Finished in 2.2s]
从结果来看,dropna行不起作用,因为行号没有改变,并且数据帧中仍然有NAN.那怎么样?我现在很疯狂.
解决方法:
您需要阅读the documentation(重点添加):
Return object with labels on given axis omitted
dropna返回一个新的DataFrame.如果您希望它修改现有的DataFrame,您需要做的就是在文档中进一步阅读:
inplace : boolean, default False
If True, do operation inplace and return None.
所以要在适当的位置修改它,请执行traindataset.dropna(how =’any’,inplace = True).