I'm getting the infamous pandas SettingWithCopyWarning when I run the following code segment:
for i in range(1, N):
if df['deltaPressure'][i] < CLUSTER_THRESHOLD:
df['Cluster'][i] = df['Cluster'][i-1]
else:
df['Cluster'][i] = df['Cluster'][i-1] + 1
I have tried fixing it by adding a .copy() as follows:
for i in range(1, N):
if df['deltaPressure'][i] < CLUSTER_THRESHOLD:
df['Cluster'][i] = df['Cluster'][i-1].copy()
else:
df['Cluster'][i] = df['Cluster'][i-1].copy() + 1
Unfortunately, I get no change to the warning. Lots of googling and searching StackOverflow has got me nowhere closer to understanding the fundamental error in my syntax or how I am inadvertently chaining. The code seems to run correctly, but I hate to ignore error messages in the hope that they will prove irrelevant.
I'd be very appreciative, both for a fix to my code, and for a simple explanation of why the .copy() does me no good.
Sincerely and with many thanks in advance
Thomas Philips