obabs
obabs

Reputation: 9171

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. 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. Try using .loc[row_indexer,col_indexer] = value instead

Upvotes: 1

Views: 2287

Answers (2)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210862

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. 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)

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. 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

Upvotes: 2

piRSquared
piRSquared

Reputation: 294348

Solution

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

pandas uses [] to provide a copy. Use loc and iloc to access the DataFrame directly.

What's more is that if the 'new' column didn't already exist, it would have worked. It only threw that error because the column already existed and you were trying to edit it on a view or copy... I think

Upvotes: 0

Related Questions