【发布时间】:2021-01-27 21:36:03
【问题描述】:
我正在尝试从熊猫官方文档中执行this。 pandas.DataFrame.fillna 所以基本上用 1 的值填充 df 数据框的“myc”列中的 NaN 值。
数据数据框
df
myc B C D
0 NaN 2.0 NaN 0
1 0.2 4.0 NaN 1
2 NaN NaN NaN 5
3 NaN 3.0 NaN 4
代码 1
values = {'myc': 1}
df.fillna(value=values)
结果目标 1
myc B C D
0 1.0 2.0 NaN 0
1 0.2 4.0 NaN 1
2 1.0 NaN NaN 5
3 1.0 3.0 NaN 4
错误消息 1
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-6a9e5a691bca> in <module>
1 values = {'myc': 1}
----> 2 df.fillna(value=values)
~/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py in fillna(self, value, method, axis, inplace, limit, downcast)
4315 downcast=None,
4316 ) -> Optional["DataFrame"]:
-> 4317 return super().fillna(
4318 value=value,
4319 method=method,
~/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py in fillna(self, value, method, axis, inplace, limit, downcast)
6071 if k not in result:
6072 continue
-> 6073 obj = result[k]
6074 obj.fillna(v, limit=limit, inplace=True, downcast=downcast)
6075 return result if not inplace else None
~/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py in __getitem__(self, key)
2876 if self.columns.nlevels > 1:
2877 return self._getitem_multilevel(key)
-> 2878 return self._get_item_cache(key)
2879
2880 # Do we have a slicer (on rows)?
~/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
3539
3540 loc = self.columns.get_loc(item)
-> 3541 values = self._mgr.iget(loc)
3542 res = self._box_col_values(values, loc)
3543
~/anaconda3/lib/python3.8/site-packages/pandas/core/internals/managers.py in iget(self, i)
986 Return the data as a SingleBlockManager.
987 """
--> 988 block = self.blocks[self.blknos[i]]
989 values = block.iget(self.blklocs[i])
990
TypeError: only integer scalar arrays can be converted to a scalar index
CODE 2 我后来也尝试列出 any_feature 列的独特功能
df['any_feature'].unique()
错误 2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-39-934988075beb> in <module>
----> 1 df['any_feature'].unique()
~/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py in __getitem__(self, key)
2876 if self.columns.nlevels > 1:
2877 return self._getitem_multilevel(key)
-> 2878 return self._get_item_cache(key)
2879
2880 # Do we have a slicer (on rows)?
~/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
3539
3540 loc = self.columns.get_loc(item)
-> 3541 values = self._mgr.iget(loc)
3542 res = self._box_col_values(values, loc)
3543
~/anaconda3/lib/python3.8/site-packages/pandas/core/internals/managers.py in iget(self, i)
986 Return the data as a SingleBlockManager.
987 """
--> 988 block = self.blocks[self.blknos[i]]
989 values = block.iget(self.blklocs[i])
990
TypeError: only integer scalar arrays can be converted to a scalar index
尝试过的解决方案
- 不是字典 - Pandas: Getting "TypeError: only integer scalar arrays can be converted to a scalar index" while trying to merge data frames
- 不是字典 - Only integer scalar arrays can be converted to a scalar index how to resolve
- 未回答 - How to resolve Python TypeError: "only integer scalar arrays can be converted to a scalar index"
- 不是字典 - TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
- 不是字典 - numpy array TypeError: only integer scalar arrays can be converted to a scalar index
- 不是字典 - TypeError: only integer scalar arrays can be converted to a scalar index when use Pandas Fillna
- 我不想转换它 - How to convert index of a pandas dataframe into a column?
- 我尝试运行以下代码来测试该对象是否为数据框,并且它说是真的,所以它是 - https://stackoverflow.com/a/14809149/10270590
- 输入
isinstance(df, pd.DataFrame) - 输出
True
- 输入
【问题讨论】:
-
你的熊猫版是什么?你的代码在我这边运行良好
-
我用 pandas 1.1.1 和 1.1.3 都检查过,pandas 没有问题。我认为您的 DF 某处有问题。也许是 numpy 版本?
-
你能分享更多代码行吗?还是数据集?
-
你能在某处分享你好 .csv 吗?如果它在另一台计算机上工作,问题将是您安装的一些软件包。
-
df['myc'] = df['myc'].fillna(1)怎么样?
标签: python python-3.x pandas dataframe dictionary