I am trying to create a new column with values that are extracted from other columns based on matching column name. For example, I have the dataframe below:
x = [1, 0.212, 0.111]
y = [0, 0.233, 0.122]
z = [1, 0.222, 0.123]
test_df = pd.DataFrame([x,y,z], columns=["Cluster","0","1"])
print(test_df)
Cluster 0 1
0 1 0.212 0.111
1 0 0.233 0.122
2 1 0.222 0.123
I want to have a new column, let's say "z" which has values from either column "0" or "1" based on the value of column "Cluster". So my expected output should be
Cluster 0 1 z
0 1 0.212 0.111 0.111
1 0 0.233 0.122 0.233
2 1 0.222 0.123 0.123
As you can see, the first row of column "z" has the same value as the first row of column "1" because the value of the first row of column "Cluster" is 1. And so on for the rest of the dataframe.
I have tried pd.lookup, for example
test_df["z"] = test_df.lookup(test_df.index,test_df["Cluster"])
But I get an error.
Please help. Thank you so much in advanced!
Regards,
Tommy