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?