0

So I am doing this super simple program and it's returning a set with copy warning. I'm using iloc.

My goal is to check if the values in check if the value is a null and replace it with a tuple with 3 None values. It does work, but I'd like to avoid the copy warning.

df = pd.DataFrame({'a':[None,1, 2], 'b':[None, (1,2,3), (3,4, 5)]}) 

    a   b
0   NaN None
1   1.0 (1, 2, 3)
2   2.0 (3, 4, 5)


if pd.isna(df['b'].iloc[0]):
        df['b'].iloc[0] = (None, None, None)
David 54321
  • 568
  • 1
  • 9
  • 23

1 Answers1

1

You can use at instead of iloc, but note that at can only work on one cell at a time (see this question).

if pd.isna(df['b'][0]):
    df.at[0,'b'] = (None, None, None)
print(df)

     a                   b
0  NaN  (None, None, None)
1  1.0           (1, 2, 3)
2  2.0           (3, 4, 5)

And you can check that it still added this as a tuple like your other row entries in b:

print(df.explode('b'))

     a     b
0  NaN  None
0  NaN  None
0  NaN  None
1  1.0     1
1  1.0     2
1  1.0     3
2  2.0     3
2  2.0     4
2  2.0     5
a11
  • 3,122
  • 4
  • 27
  • 66