与DataFrame不兼容的索引器
我试图根据另一列的条件在一个列中设置值(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您只想填充
RoomService
列,您可以做Since you only want to fillna on
RoomService
column, you can do