简体   繁体   中英

Pandas: From SettingWithCopyWarning to loc and iloc

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM