0

I am trying to create two new columns in my dataframe depending on the values of the columns Subscribers, External Party and Direction. If the Direction is I for Incoming, column a should become External Party and col B should become Subscriber. If the Direction is O for Outgoing, it should be the other way around. I use the code:

import pandas as pd
import numpy as np
...
df['a'] = np.where((df.Direction == 'I'), df['External Party'], df['Subscriber'])
df['b'] = np.where((df.Direction == 'O'), df['External Party'], df['Subscriber'])

I get a SettingWithCopyWarning from Pandas, but the code does what it needs to do. How can I improve this operation to avoid the error? Thanks in advance! Jo

Jo_
  • 81
  • 6

2 Answers2

0

Inspect the place in your code where df is created.

Most probably, it is a view of another DataFrame, something like:

df = df_src[...]

Then any atempt to save something in df causes just this warning.

To avoid it, create df as a truly independent DataFrame, with its own data buffer. Something like:

df = df_src[...].copy()

Now df has its own data buffer, and can be modified without the above warning.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
0

If you are planning to work with the same df later on in your code then it is sometimes useful to create a deep copy of the df before making any iterations.

Pandas native copy method is not always acting as one would expect - here is a similar question that might give more insights.

You can use copy module that comes with python to copy the entire object and to ensure that there are no links between 2 dataframes.

import copy
df_copy = copy.deepcopy(df)
olv1do
  • 96
  • 6