简体   繁体   English

将默认值设置为Pandas数据框中的整个新列时出错

[英]Error when setting default value to entire new column in Pandas dataframe

Code works however getting this error when trying to set default value =1 to entire new column in Pandas dataframe. 尝试将默认值= 1设置为Pandas数据框中的整个新列时,代码会工作,但会出现此错误。 What does this warning error mean and how can I rework it so I don't get this warning error. 此警告错误是什么意思,我该如何进行重新处理,以免出现此警告错误。

df['new']=1

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

this should solve the problem: 这应该可以解决问题:

soldactive = df[(df.DispositionStatus == 'Sold') & (df.AssetStatus == 'Active')].copy()

your code: 您的代码:

removesold = df(df.ExitDate.isin(errorval)) & (df.DispositionStatus == 'Sold') & (af.AssetStatus == 'Resolved')]
df = df.drop(removesold.index)
soldactive = df[(df.DispositionStatus == 'Sold') & (df.AssetStatus == 'Active')]
soldactive['FlagError'] = 1 

you've created soldactive DF as a copy of the subset ( sliced ) df . 您已经创建了soldactive DF作为子集( 切片df的副本。 After that you'are trying to create a new column on that copy. 之后,您要尝试在该副本上创建一个新列。 It gives you a warning: A value is trying to be set on a copy of a slice from a DataFrame because dataframes are value-mutable (see excerpt from docs below) 它会向您发出警告:由于数据帧是值可变的, A value is trying to be set on a copy of a slice from a DataFrame值(请参阅以下文档的摘录)

Docs: 文件:

All pandas data structures are value-mutable (the values they contain can be altered) but not always size-mutable. 所有的熊猫数据结构都是值可变的(它们包含的值可以更改),但并不总是大小可变的。 The length of a Series cannot be changed, but, for example, columns can be inserted into a DataFrame. 系列的长度不能更改,但是,例如,可以将列插入到DataFrame中。 However, the vast majority of methods produce new objects and leave the input data untouched. 但是,绝大多数方法都会产生新对象,并保持输入数据不变。 In general, though, we like to favor immutability where sensible. 总的来说,尽管如此,我们还是希望在合理的地方支持不变性。

Here is a test case: 这是一个测试案例:

In [375]: df
Out[375]:
   a  b  c
0  9  6  4
1  5  2  8
2  8  1  6
3  3  4  1
4  8  0  2

In [376]: a = df[1:3]

In [377]: a['new'] = 1
C:\envs\py35\Scripts\ipython:1: 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

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

In [378]: del a

In [379]: a = df[1:3].copy()

In [380]: a['new'] = 1

In [381]: a
Out[381]:
   a  b  c  new
1  5  2  8    1
2  8  1  6    1

In [382]: df
Out[382]:
   a  b  c
0  9  6  4
1  5  2  8
2  8  1  6
3  3  4  1
4  8  0  2

Solution

df.loc[:, 'new'] = 1

pandas uses [] to provide a copy. pandas使用[]提供副本。 Use loc and iloc to access the DataFrame directly. 使用lociloc访问DataFrame直接。

What's more is that if the 'new' column didn't already exist, it would have worked. 更重要的是,如果'new'列不存在,它将会起作用。 It only threw that error because the column already existed and you were trying to edit it on a view or copy... I think 它仅引发该错误,因为该列已存在,并且您试图在视图或副本上对其进行编辑...我认为

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM