简体   繁体   English

SettingWithCopyWarning 即使使用.loc[row_indexer,col_indexer] = value

[英]SettingWithCopyWarning even when using .loc[row_indexer,col_indexer] = value

This is one of the lines in my code where I get the SettingWithCopyWarning :这是我获得SettingWithCopyWarning的代码行之一:

value1['Total Population']=value1['Total Population'].replace(to_replace='*', value=4)

Which I then changed to:然后我改为:

row_index= value1['Total Population']=='*'
value1.loc[row_index,'Total Population'] = 4

This still gives the same warning.这仍然给出相同的警告。 How do I get rid of it?我该如何摆脱它?

Also, I get the same warning for a convert_objects(convert_numeric=True) function that I've used, is there any way to avoid that.另外,对于我使用过的convert_objects(convert_numeric=True) function,我收到了相同的警告,有什么办法可以避免这种情况。

 value1['Total Population'] = value1['Total Population'].astype(str).convert_objects(convert_numeric=True)

This is the warning message that I get:这是我收到的警告消息:

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 the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy 

If you use .loc[row,column] and still get the same error, it's probably because of copying another data frame.如果您使用.loc[row,column]并仍然得到相同的错误,则可能是因为复制了另一个数据框。 You have to use .copy() .你必须使用.copy()

This is a step by step error reproduction:这是一步一步的错误再现:

import pandas as pd

d = {'col1': [1, 2, 3, 4], 'col2': [3, 4, 5, 6]}
df = pd.DataFrame(data=d)
df
#   col1    col2
#0  1   3
#1  2   4
#2  3   5
#3  4   6

Creating a new column and updating its value:创建一个新列并更新其值:

df['new_column'] = None
df.loc[0, 'new_column'] = 100
df
#   col1    col2    new_column
#0  1   3   100
#1  2   4   None
#2  3   5   None
#3  4   6   None

No error I receive.我没有收到错误。 However, let's create another data frame given the previous one:但是,让我们根据前一个数据框创建另一个数据框:

new_df = df.loc[df.col1>2]
new_df
#col1   col2    new_column
#2  3   5   None
#3  4   6   None

Now, using .loc , I will try to replace some values in the same manner:现在,使用.loc ,我将尝试以相同的方式替换一些值:

new_df.loc[2, 'new_column'] = 100

However, I got this hateful warning again:但是,我再次收到了这个可恶的警告:

A value is trying to be set on a copy of a slice from a DataFrame.试图在来自 DataFrame 的切片副本上设置值。 Try using .loc[row_indexer,col_indexer] = value instead尝试使用 .loc[row_indexer,col_indexer] = value 代替

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy请参阅文档中的警告: https : //pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

SOLUTION解决方案

use .copy() while creating the new data frame will solve the warning:在创建新数据框时使用.copy()将解决警告:

new_df_copy = df.loc[df.col1>2].copy()
new_df_copy.loc[2, 'new_column'] = 100

Now, you won't receive any warnings!现在,您将不会收到任何警告!

If your data frame is created using a filter on top of another data frame, always use .copy() .如果您的数据框是使用另一个数据框顶部的过滤器创建的,请始终使用.copy()

您是否尝试过直接设置?:

value1.loc[value1['Total Population'] == '*', 'Total Population'] = 4

I came here because I wanted to conditionally set the value of a new column based on the value in another column.我来这里是因为我想根据另一列中的值有条件地设置新列的值。

What worked for me was numpy.where:对我有用的是 numpy.where:

import numpy as np
import pandas as pd
...

df['Size'] = np.where((df.value > 10), "Greater than 10", df.value)

From numpy docs , this is equivelant to:numpy docs ,这相当于:

[xv if c else yv
 for c, xv, yv in zip(condition, x, y)]

Which is a pretty nice use of zip...这是 zip 的一个很好的用法......

I have no idea how bad the data storage/memory implications are with this but it fixes it every time for your average dataframe:我不知道这对数据存储/内存的影响有多糟糕,但它每次都会为您的平均数据帧修复它:

def addCrazyColFunc(df):
    dfNew = df.copy()
    dfNew['newCol'] = 'crazy'
    return dfNew

Just like the message says... make a copy and you're good to go.就像消息说的那样......制作一份副本,你就可以开始了。 Please if someone can fix the above without the copy, please comment.请如果有人可以在没有副本的情况下解决上述问题,请发表评论。 All the above loc stuff doesn't work for this case.上面所有的 loc 东西都不适用于这种情况。

Try adding the following before the line where the warning is raised (:reindexing if necessary).尝试在发出警告的行之前添加以下内容(如有必要:重新索引)。 It has the same effect as df.copy() , so there will be no warning.它与df.copy()具有相同的效果,因此不会有警告。

 df = df.reset_index(drop=True) 

Got the solution:得到了解决方案:

I created a new DataFrame and stored the value of only the columns that I needed to work on, it gives me no errors now!我创建了一个新的 DataFrame 并仅存储了我需要处理的列的值,现在它没有给我任何错误!

Strange, but worked.奇怪,但有效。

Specifying it is a copy worked for me.指定它是为我工作的副本。 I just added .copy() at the end of the statement我刚刚在语句的末尾添加了.copy()

value1['Total Population'] = value1['Total Population'].replace(to_replace='*', value=4).copy()

This should fix your problem:这应该可以解决您的问题:

value1[:, 'Total Population'] = value1[:, 'Total Population'].replace(to_replace='*', value=4)

I was able to avoid the same warning message with syntax like this:我能够避免使用以下语法出现相同的警告消息:

value1.loc[:, 'Total Population'].replace('*', 4)

Note that the dataframe doesn't need to be re-assigned to itself, ie value1['Total Population']=value1['Total Population']... 请注意,不需要将数据帧重新分配给自身,即 value1['Total Population']=value1['Total Population']...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 .loc[row_indexer,col_indexer] = value 更新列时收到警告 - Getting a warning when updating a column using .loc[row_indexer,col_indexer] = value SettingWithCopyWarning:试图在 DataFrame 中切片的副本上设置值。 尝试使用 .loc[row_indexer,col_indexer] = value 代替, - SettingWithCopyWarning: 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 之后也尝试使用 .loc[row_indexer,col_indexer] 错误 - Try using .loc[row_indexer,col_indexer] error even after using .loc 熊猫尝试改用.loc [row_indexer,col_indexer] = value - Pandas Try using .loc[row_indexer,col_indexer] = value instead 再次尝试使用 .loc[row_indexer,col_indexer] = value - Try using .loc[row_indexer,col_indexer] = value instead again 警告:尝试使用 .loc[row_indexer,col_indexer] = value 代替 - Warning : Try using .loc[row_indexer,col_indexer] = value instead 尝试改用.loc [row_indexer,col_indexer] = value - Try using .loc[row_indexer,col_indexer] = value instead fit.resid给出错误'尝试使用.loc [row_indexer,col_indexer] = value而不是'? - fit.resid gives error 'Try using .loc[row_indexer,col_indexer] = value instead'? Pandas | 替代 lambda function =>.loc[row_indexer,col_indexer] = value - Pandas | Alternative for lambda function => .loc[row_indexer,col_indexer] = value instead 试图在 DataFrame 的切片副本上设置值。 尝试改用 .loc[row_indexer,col_indexer] = value - 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM