I am trying to do this from the official pandas documentation. pandas.DataFrame.fillna So Basicly filling up the NaN values in the df dataframe's "myc" column with values of 1.
DATA dataframe
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
CODE 1
values = {'myc': 1}
df.fillna(value=values)
Results Goal 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
ERROR MESAGE 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 I have also later on tried to list out the unique features for the any_feature column
df['any_feature'].unique()
ERROR 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
Tried Solutions
- Not dictionary - Pandas: Getting "TypeError: only integer scalar arrays can be converted to a scalar index" while trying to merge data frames
- Not dictionary - Only integer scalar arrays can be converted to a scalar index how to resolve
- not answered - How to resolve Python TypeError: "only integer scalar arrays can be converted to a scalar index"
- Not dictionary - TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
- Not dictionary - numpy array TypeError: only integer scalar arrays can be converted to a scalar index
- Not dictionary - TypeError: only integer scalar arrays can be converted to a scalar index when use Pandas Fillna
- I don't want to convert it - How to convert index of a pandas dataframe into a column?
- I have tried running the following code to test if that object is a dataframe and it said true so it is - https://stackoverflow.com/a/14809149/10270590
- INPUT
isinstance(df, pd.DataFrame)
- OUTPUT
True
- INPUT