I have a DataFrame with lots of columns that I want to reorganize the order of some columns(not all of them), depending on the value of some other.
I tried to change the order with .loc, when I run the code, it shows what I want, but when I try to set the values with it, it doesn't.
Example, I want to change only the order of the Math_answers that have type_Math A, so the last 2 Ans_Math would be: 'ABC' and 'ACB'
import pandas as pd
import numpy as np
data = pd.DataFrame({'Type_Bio':['A','A','B','A','B'],
'Type_Math':['B','B','B','A','A'],
'Ans_Bio':['AEC','ABC','AAC','CBA','BCA'],
'Ans_Math':['AEC','ABC','AAC','CBA','BCA']})
data
Type_Bio Type_Math Ans_Bio Ans_Math
0 A B AEC AEC
1 A B ABC ABC
2 B B AAC AAC
3 A A CBA CBA
4 B A BCA BCA
I used this code to split the Math answers:
data = pd.concat([data, data.loc[:,'Ans_Math'].str.split('', expand = True).iloc[:,1:4]], axis = 1)
Type_Bio Type_Math Ans_Bio Ans_Math 1 2 3
0 A B AEC AEC A E C
1 A B ABC ABC A B C
2 B B AAC AAC A A C
3 A A CBA CBA C B A
4 B A BCA BCA B C A
Then, I noticed that:
data.loc[data.Type_Math == 'A',np.arange(1, 4)][np.arange(3,0,-1)]
Gives what I want(almost)(Only the lines I that want, reversed!)
3 2 1
3 A B C
4 A C B
So, the next step is to set the values:
data.loc[data.Type_Math == 'A',np.arange(1, 4)] = data.loc[data.Type_Math == 'A',np.arange(1, 4)][np.arange(3,0,-1)]
data
But the data itself has not changed:
Type_Bio Type_Math Ans_Bio Ans_Math 1 2 3
0 A B AEC AEC A E C
1 A B ABC ABC A B C
2 B B AAC AAC A A C
3 A A CBA CBA C B A
4 B A BCA BCA B C A
I am aware that are some bugs with setting with copy, but i thought using .loc would not have this issue, somebody could explain and help please?