与 pandas 的对话SettingWithCopyWarning [英] Action with pandas SettingWithCopyWarning
问题描述
我尝试删除
一些列,并将列中的某些值转换为
df2.drop(df2.columns [[0,1,3]],axis = 1,inplace = True)
df2 ['date'] = df2 ['date']。 :str(x)[1:])
df2 ['date'] = df2 ['date']。str.replace(':','',1)
df2 ['date' ] = pd.to_datetime(df2 ['date'])
和所有这个字符串我得到
df2.drop(df2.columns [[0,1,3]],axis = 1,inplace = True)
C:/ Users / / Desktop / projects / youtube_log / filter.py:11:SettingWithCopyWarning:
正在尝试设置一个值DataFrame中的切片副本。
尝试使用.loc [row_indexer,col_indexer] =值而不是
那里有什么问题?
您的 df2
是另一个数据帧的切片。在尝试 drop
df2 = df2.copy()
> 考虑以下数据框:
将大熊猫导入为pd
导入numpy as np
df1 = pd.DataFrame(np.arange(20).reshape(4,5),list('abcd'),list('ABCDE'))
df1
让我分配一个 df1
到 df2
df2 = df1 [['A','C']]
df2
现在是一个 df1
的切片,应该触发那些讨厌的 SettingWithCopyWarning
如果我们尝试更改 df2
中的内容。让我们来看看。
df2.drop('c')
pre>
没有问题。如何:
df2.drop('c',inplace = True)
它是:
问题是大熊猫试图有效率,并追踪
df2
指向与$ $ c $相同的数据c> df1 。正在保持这种关系。警告告诉你,你不应该试图通过切片弄乱原始的数据框。
注意,当我们看看
df2
,行'c'已被删除。df2
code> df1 我们看到那行'c'仍然存在。
df1
$ b $大熊猫制作了df2
的副本,然后删除c行。这可能与我们的意图可能与我们的意图可能不一致,我们认为我们将df2
一个切片并指向与df1
。所以熊猫正在警告我们。
如果没有看到警告,请自己复制。
df2 = df2.copy()
#或
df2 = df1 [['A','C']]。copy()
I try to
delete
some column and convert some value in column withdf2.drop(df2.columns[[0, 1, 3]], axis=1, inplace=True) df2['date'] = df2['date'].map(lambda x: str(x)[1:]) df2['date'] = df2['date'].str.replace(':', ' ', 1) df2['date'] = pd.to_datetime(df2['date'])
and to all this string I get
df2.drop(df2.columns[[0, 1, 3]], axis=1, inplace=True) C:/Users/����� �����������/Desktop/projects/youtube_log/filter.py:11: 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
What is problem there?
解决方案Your
df2
is a slice of another dataframe. You need to explicitly copy it withdf2 = df2.copy()
just prior to your attempt todrop
Consider the following dataframe:
import pandas as pd import numpy as np df1 = pd.DataFrame(np.arange(20).reshape(4, 5), list('abcd'), list('ABCDE')) df1
Let me assign a slice of
df1
todf2
df2 = df1[['A', 'C']]
df2
is now a slice ofdf1
and should trigger those peskySettingWithCopyWarning
's if we try to change things indf2
. Let's take a look.df2.drop('c')
No problems. How about:
df2.drop('c', inplace=True)
There it is:
The problem is that pandas tries to be efficient and tracks that
df2
is pointing to the same data asdf1
. It is preserving that relationship. The warning is telling you that you shouldn't be trying to mess with the original dataframe via the slice.Notice that when we look at
df2
, row 'c' has been dropped.df2
And looking at
df1
we see that row 'c' is still there.df1
pandas made a copy of
df2
then dropped row 'c'. This is potentially inconsistent with what our intent may have been considering we madedf2
a slice of and pointing to same data asdf1
. So pandas is warning us.To not see the warning, make the copy yourself.
df2 = df2.copy() # or df2 = df1[['A', 'C']].copy()
这篇关于与 pandas 的对话SettingWithCopyWarning的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!