Skip to content Skip to sidebar Skip to footer

Python Pandas Removing SettingWithCopyWarning

So I made an empty dataframe using df=data[['ID','Matrix','Name','Country', 'Units']] df['Value']='' and I am filling it in with code like this, which finds strings containing val

Solution 1:

Here is a good explanation of why this warning was turned on:

Pandas: Chained assignments

Are you sure that is all of your code? Pls show all of what you are doing.

In [13]: df = DataFrame(index=range(5))

In [14]: df['Value'] = ''

In [15]: df.loc[[1,4],'Value'] = 'bad'

In [16]: df.loc[[0,3],'Value'] = 'good'

In [17]: df
Out[17]: 
  Value
0  good
1   bad
2      
3  good
4   bad

[5 rows x 1 columns]

2nd example

In [1]: df = DataFrame(index=range(5))

In [2]: df['Value'] = ''

In [3]: df2 = DataFrame(dict(A=['foo','foo','bar','bar','bah']))

In [4]: df
Out[4]: 
  Value
0      
1      
2      
3      
4      

[5 rows x 1 columns]

In [5]: df2
Out[5]: 
     A
0  foo
1  foo
2  bar
3  bar
4  bah

[5 rows x 1 columns]

In [6]: df.loc[df2.A.str.contains('foo'),'Value'] = 'good'

In [7]: df.loc[df2.A.str.contains('bar'),'Value'] = 'bad'

In [8]: df
Out[8]: 
  Value
0  good
1  good
2   bad
3   bad
4      

[5 rows x 1 columns]

Post a Comment for "Python Pandas Removing SettingWithCopyWarning"