4

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?

Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
ramesh
  • 1,187
  • 7
  • 19
  • 42

2 Answers2

2

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.

piRSquared
  • 285,575
  • 57
  • 475
  • 624
0

Chained assignment will now by default warn if the user is assigning to a copy.

This can be changed with the option mode.chained_assignment, allowed options are raise/warn/None. See the docs.

In [5]: dfc = DataFrame({'A':['aaa','bbb','ccc'],'B':[1,2,3]})

In [6]: pd.set_option('chained_assignment','warn')

The following warning / exception will show if this is attempted.

In [7]: dfc.loc[0]['A'] = 1111

Traceback (most recent call last) ... SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_index,col_indexer] = value instead Here is the correct method of assignment.

In [8]: dfc.loc[0,'A'] = 11

In [9]: dfc

 A  B

0 11 1

1 bbb 2

2 ccc 3

Lin
  • 1
  • 1