[英]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. 当我在1K行上工作时,它可以工作,但我有
SettingWithCopyWarning
那么当我在2M行上工作时,则无法工作。
Could you please help me to fix that and maybe to convert it with loc
/ iloc
? 你能帮我解决这个问题,也许可以用
loc
/ iloc
转换吗?
input1['Signature_Fixed'][i]
represents chained indexing, which is explicitly discouraged in the official documentation. input1['Signature_Fixed'][i]
表示链接索引,在官方文档中明确建议不要使用此索引。 Avoid it wherever possible. 尽可能避免它。
In this case, you can avoid for
loops altogether by using pd.Series.mask
: 在这种情况下,可以使用
pd.Series.mask
完全避免for
循环:
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. 语法上的想法是以向量化的方式对列进行操作,而不是按行循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.