Search code examples
pythonpandaschained-assignment

Python: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame


My pandas dataframe:

dframe = pd.DataFrame({"A":list("abcde"), "B":list("aabbc"), "C":[1,2,3,4,5]},  index=[10,11,12,13,14])

    A   B   C
10  a   a   1
11  b   a   2
12  c   b   3
13  d   b   4
14  e   c   5

My desired output:

    A   B   C   a   b   c
10  a   a   1   1   None    None
11  b   a   2   2   None    None
12  c   b   3   None    3   None
13  d   b   4   None    4   None
14  e   c   5   None    None    5

Idea is to create new column based on values in 'B' column, copy respective values in 'C' column and paste them in newly created columns. Here is my code:

lis = sorted(list(dframe.B.unique()))

#creating empty columns
for items in lis:
   dframe[items] = None


 #here copy and pasting
    for items in range(0, len(dframe)):
        slot = dframe.B.iloc[items]
        dframe[slot][items] = dframe.C.iloc[items]

I ended up with this error:

SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  app.launch_new_instance()

This code worked well in Python 2.7 but not in 3.x. Where I'm going wrong?


Solution

  • Start with

    to_be_appended = pd.get_dummies(dframe.B).replace(0, np.nan).mul(dframe.C, axis=0)
    

    Then concat

    dframe = pd.concat([dframe, to_be_appended], axis=1)
    

    Looks like:

    print dframe
    
        A  B  C    a    b    c
    10  a  a  1  1.0  NaN  NaN
    11  b  a  2  2.0  NaN  NaN
    12  c  b  3  NaN  3.0  NaN
    13  d  b  4  NaN  4.0  NaN
    14  e  c  5  NaN  NaN  5.0
    

    Notes for searching.

    This is combining one hot encoding with a broadcast multiplication.