A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy super().__setitem__(key, value) Traceback (most recent call last): File "E:\code-study\coda\cross_nostopline.py", line 75, in <module> print(value[1]) TypeError: 'Polygon' object is not subscriptable
时间: 2024-04-25 18:25:53 浏览: 9
这个错误通常是由pandas的一个警告引起的,而不是错误。警告通知您正在对DataFrame的副本进行更改。
在您的代码中,您可能使用了类似于这样的语句:
```
df[col_name][row_index] = new_value
```
这会生成DataFrame的副本并更改副本,而不是更改原始DataFrame。为了避免这个警告,您可以使用.loc访问器:
```
df.loc[row_index, col_name] = new_value
```
如果您仍然遇到问题,并得到类似于“'Polygon' object is not subscriptable”这样的错误,那么这意味着您正在尝试对一个不可索引的对象进行索引。请检查您的代码并确定您是否尝试对Polygon对象进行索引。如果是这个问题,您需要找到正确的方法来访问Polygon对象的属性。
相关问题
A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead
这个警告通常出现在使用 Pandas DataFrame 进行数据操作时,意味着你正在尝试在 DataFrame 的切片副本上设置值。为了避免这个警告,建议你使用 .loc[row_indexer, col_indexer] = value 来设置值,这样可以确保在原始 DataFrame 上进行操作。
举个例子,如果你有一个 DataFrame df,并且想要在特定行和列上设置数值,你可以使用以下方式来避免这个警告:
```python
df.loc[row_index, col_index] = value
```
这样就可以避免警告并且正确地在原始 DataFrame 上设置数值了。
提示错误:A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead
这个错误通常发生在尝试通过切片修改DataFrame的值时。出现这个错误的原因是切片返回的是原始数据的视图,而不是副本,所以在修改时可能会影响原始数据。为了避免这个错误,可以使用.loc[row_indexer,col_indexer] = value来直接对DataFrame进行修改,这样就能够避免对原始数据造成影响。
例如,如果你想要将一个DataFrame中的某一列中的所有值都设置为0,可以这样做:
```python
import pandas as pd
# 创建一个DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# 错误的做法
df['A'][:] = 0
# 正确的做法
df.loc[:, 'A'] = 0
```
上面的错误的做法会导致提示错误:A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead。而正确的做法则使用了.loc[row_indexer,col_indexer]来直接操作DataFrame。