gpt4 book ai didi

python - 不能在 Pandas 中使用 dropna 删除 NAN

转载 作者:太空狗 更新时间:2023-10-30 01:41:01 26 4
gpt4 key购买 nike

我将 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)

关于python - 不能在 Pandas 中使用 dropna 删除 NAN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33643843/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com