与DataFrame不兼容的索引器

发布于 2025-02-04 05:26:40 字数 2513 浏览 1 评论 0原文

我试图根据另一列的条件在一个列中设置值(HomePlanet是一个,而RoomService是另一个):

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

我有一个错误:

 ---------------------------------------------------------------------------
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

令人困惑的是,我在其他类似数据上做同样的事情,并且没问题。 有人知道如何解决这个问题吗?

这是我的数据框架的信息:

 <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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九歌凝 2025-02-11 05:26:40

由于您只想填充RoomService列,您可以做

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

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

test.loc[test.HomePlanet == 1, 'RoomService'] = test['RoomService'].fillna(135)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文