Working with pandas dataframe suppose i have data frame with similar structure as following:
import pandas as pd
a_choise = ["True", "False", "False", "False", "True", "False", "False", "True", "True"]
b_choise = ["True", "True", "False", "False", "False", "False", "True", "True", "True"]
c_choise = ["False", "False", "True", "False", "True", "True", "False", "True", "False"]
a_n = ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9"]
b_n = ["b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9"]
c_n = ["c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9"]
df = pd.DataFrame(
{"a": list(range(1, 10)), "b": list(range(11, 20)), "c": range(21, 30),
"a_Cho":a_choise, "b_Cho":b_choise, "c_Cho":c_choise,
"a_n":a_n, "b_n":b_n, "c_n":c_n}
)
a b c a_Cho b_Cho c_Cho a_n b_n c_n
0 1 11 21 True True False a1 b1 c1
1 2 12 22 False True False a2 b2 c2
2 3 13 23 False False True a3 b3 c3
3 4 14 24 False False False a4 b4 c4
4 5 15 25 True False True a5 b5 c5
5 6 16 26 False False True a6 b6 c6
6 7 17 27 False True False a7 b7 c7
7 8 18 28 True True True a8 b8 c8
8 9 19 29 True True False a9 b9 c9
I want a new 2 columns (Choise, Value) that meet the follwoing conditions for all values in "a_Cho", "b_Cho", And "c_Cho"
- if "a_Cho" = true then choise = "a_n", value = a for the corresponding value of "a_Cho" elif "a_Cho" = false then move to next
- if "b_Cho" = true then choise = "b_n", value = b for the corresponding value of "b_Cho"elif "b_Cho" = false then move to next
- if "c_Cho" = true then choise = "c_n", value = c for the corresponding value of "c_Cho"elif "c_Cho" = false then move to next
- if "x_Cho" = false then value and choise = "Invalide"