【发布时间】:2020-07-11 04:00:31
【问题描述】:
在 Pandas 中,从列中删除 NaN 值后,存储在删除 NaN 值的索引处的值是多少?我能够成功地从列中删除 NaN 值,但 df 的形状是完整的,但该特定列的大小发生了变化。
1445 70.0
**1446 NaN**
1447 80.0
1448 70.0
1449 21.0
1450 60.0
1451 78.0
1452 35.0
1453 90.0
1454 62.0
1455 62.0
1456 85.0
1457 66.0
1458 68.0
1459 75.0
Name: LotFrontage, dtype: float64
Size of LotFrontage before removing NaN values: 1460
这是删除 NaN 值后得到的结果
1444 63.0
1445 70.0
1447 80.0
1448 70.0
1449 21.0
1450 60.0
1451 78.0
1452 35.0
1453 90.0
1454 62.0
1455 62.0
1456 85.0
1457 66.0
1458 68.0
1459 75.0
Name: LotFrontage, dtype: float64
New size of LotFrontage after removing NaN values: 1201
尝试分配索引 1446 的值时出现以下错误:
[在此处输入图片描述][1]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-70-7cb9d14fb3e0> in <module>()
3 print("New size of LotFrontage after revoving NaN values: " + str(iowa['LotFrontage'].size))
4 print(iowa['LotFrontage'][1445])
----> 5 print(iowa['LotFrontage'][1446])
1 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_value(self, series, key)
4403 k = self._convert_scalar_indexer(k, kind="getitem")
4404 try:
-> 4405 return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
4406 except KeyError as e1:
4407 if len(self) > 0 and (self.holds_integer() or self.is_boolean()):
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 1446
【问题讨论】:
-
我认为您的意思是“在 Pandas 中”而不是“在 Python 中”,对吧?如果是这样,请相应地编辑您的问题和标签。
-
是的。我是说熊猫