I have a dataframe in which I want to create a new column based on the values stored in another column.
foo = pd.DataFrame(
[['USA','x',1,2],
['Canada','y',2,4],
['Australia','x',3,6]],
columns = ('Country', 'C1','x', 'y')
)
For example, given following dataframe
Country C1 x y
0 USA x 1 2
1 Canada y 2 4
2 Australia x 3 6
I want to create a new column say z but the column C1 decides where the value of z will be coming from column x or column y. In other words, I want to do something like
foo['z']=foo[foo['C1']]
The column C1 can have one of 28 different values. The question is different from the question referred in the comments as I do not want to calculate values on the basis of pre existing values rather the value in one of the column ( C1 in this case) contains the name of the column whose value should be stored in the new column.