Home > OS >  Incompatible indexer with DataFrame
Incompatible indexer with DataFrame

Time:06-05

I was trying to set values in one column based on conditions from another column(HomePlanet is one and RoomService is the other one):

     test.loc[test.HomePlanet == 1, 'RoomService'] = test.fillna(135)

I got an error:

 ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_2477/695172142.py in <module>
----> 1 test.loc[test.HomePlanet == 1, 'RoomService'] = test.fillna(135)

~/anaconda3/lib/python3.9/site-packages/pandas/core/indexing.py in __setitem__(self, key, value)
721 
722         iloc = self if self.name == "iloc" else self.obj.iloc
--> 723         iloc._setitem_with_indexer(indexer, value, self.name)
724 
725     def _validate_key(self, key, axis: int):

~/anaconda3/lib/python3.9/site-packages/pandas/core/indexing.py 
in _setitem_with_indexer(self, indexer, value, name)

1730             self._setitem_with_indexer_split_path(indexer, value, name)
1731         else:
-> 1732             self._setitem_single_block(indexer, value, name)
1733 
1734     def _setitem_with_indexer_split_path(self, indexer, value, name: str):

~/anaconda3/lib/python3.9/site-packages/pandas/core/indexing.py in _setitem_single_block(self, indexer, value, name)
1960 
1961         elif isinstance(value, ABCDataFrame) and name != "iloc":
-> 1962             value = self._align_frame(indexer, value)
1963 
1964         # check for chained assignment

~/anaconda3/lib/python3.9/site-packages/pandas/core/indexing.py in _align_frame(self, indexer, df)
2199             return val
2200 
-> 2201         raise ValueError("Incompatible indexer with DataFrame")
2202 
2203 

ValueError: Incompatible indexer with DataFrame

What is confusing is that I do the same thing on my other similar data, and have no problem. Does anyone know how to solve this?

This is the info of mine data frame:

 <class 'pandas.core.frame.DataFrame'>
  RangeIndex: 4277 entries, 0 to 4276
  Data columns (total 10 columns):
  #   Column        Non-Null Count  Dtype  
 ---  ------        --------------  -----  
  0   HomePlanet    4190 non-null   float64
  1   CryoSleep     4184 non-null   float64
  2   Destination   4185 non-null   float64
  3   Age           4186 non-null   float64
  4   VIP           4184 non-null   float64
  5   RoomService   4195 non-null   float64
  6   FoodCourt     4171 non-null   float64
  7   ShoppingMall  4179 non-null   float64
  8   Spa           4176 non-null   float64
  9   VRDeck        4197 non-null   float64
  dtypes: float64(10)
  memory usage: 334.3 KB

CodePudding user response:

Since you only want to fillna on RoomService column, you can do

test.loc[test.HomePlanet == 1, 'RoomService'] = test['RoomService'].fillna(135)
  • Related