0

I am having an issue with SettingWithCopyWarning and values not being replaced. I searched on this site pretty extensively, and all I'm seeing in terms of dealing with SettingWithCopyWarning is to use copy(). However, trying to replace any missing values in col1 with the values in col2 even with the following code is not working for me-

import numpy as  np
import pandas as pd
import copy
test = pd.DataFrame({'a':[1,np.nan,3], 'b':[5,6,7]})
test2 = test.copy()
test3 = test.copy()
test2.loc[np.isnan(test2['a']) & (np.isnan(test2['b']) == False)]['a'] = 
    test3.loc[np.isnan(test3['a']) & (np.isnan(test3['b']) == False)]['b']

print(test2)

The NaN in the first column is still there, and I am still getting the SettingWithCopyWarning. What am I missing?

Aftab H.
  • 1,517
  • 4
  • 13
  • 25
JTron
  • 23
  • 3

1 Answers1

0

I believe you need:

test = pd.DataFrame({'a':[1,np.nan,3], 'b':[5,6,7]})

test['a'] = test['a'].fillna(test['b'])
#alternative
#test['a'] = test['a'].combine_first(test['b'])
print (test)
     a  b
0  1.0  5
1  6.0  6
2  3.0  7

EDIT:

Solution with loc is really similar, only remove ][ for avoid chaining indexing:

test2.loc[np.isnan(test2['a']) & (np.isnan(test2['b']) == False), 'a'] = \
    test3.loc[np.isnan(test3['a']) & (np.isnan(test3['b']) == False), 'b']
print (test2)
     a  b
0  1.0  5
1  6.0  6
2  3.0  7
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • That seems like a nice workaround (which works fine), but what did I miss in terms of using loc? – JTron Mar 04 '18 at 17:48