1

I tried to do:

input1['Signature_Fixed'] = 'NONE' 
i = 0

for row in input1['Signature']:
  if (row == 'Competitor'):
    input1['Signature_Fixed'][i] = input1['brand'][i]
  else:
    input1['Signature_Fixed'][i] = input1['Signature'][i]
  i = i + 1

When I am doing on 1K rows, it works but I have SettingWithCopyWarning then when I am doing on 2M rows, is not working.

Could you please help me to fix that and maybe to convert it with loc/iloc?

Georgy
  • 12,464
  • 7
  • 65
  • 73
Joseph E
  • 13
  • 3

1 Answers1

0

input1['Signature_Fixed'][i] represents chained indexing, which is explicitly discouraged in the official documentation. Avoid it wherever possible.

In this case, you can avoid for loops altogether by using pd.Series.mask:

bool_mask = df['Signature'] == 'Competitor'
df['Signature_Fixed'] = df['Signature'].mask(bool_mask, df['brand'])

The idea in syntax terms is to operate on columns in vectorised fashion rather than on row-wise loops.

jpp
  • 159,742
  • 34
  • 281
  • 339