Reputation: 4116
I am trying to add and fill a column in a Pandas
DataFrame
based on a series of if conditions.
My code looks like this
df['E'] = (('Text1;' if df['A'] else '') + ('Text2;' if df['B'] else '')) + ('Text3;' if df['C'] else '')
The dataframe looks like
A B C D
TRUE FALSE FALSE 4
FALSE TRUE TRUE 7
FALSE FALSE FALSE 1
compiler errors out saying Truth Value being Ambiguous, I tried playing with parenthesis as suggested by SO but cant get this to work.
I have checked many other threads on SO around this error but none point to this problem.
Thanks
Upvotes: 1
Views: 76
Reputation:
Your way not working because: if else with series and dfs are not good and there are many many QAs why it is wrong and what to do.
but still you could try np.select
with 2^3 possibilities after checking them but you didn't show such attempt.
theres another way with matrix mul:
#pd.Index plays better than list when it comes to @ below
texts = pd.Index(["Text1;", "Text2;","Text3;"])
#choose interested columns
columns = ["A","B","C"]
#perform matrix mult and assign
df["E"] = df[columns] @ texts
matrix multiplication here with @ is "sum of products". It produces 3 such sum, one is for example "Text1;" * True + "Text2;" * False + "Text3;" * False
for the first E result. this is usual matrix multiplcation as you might know
df becomes
A B C D E
0 True False False 4 Text1;
1 False True True 7 Text2;Text3;
2 False False False 1
2^3 way with np.select. we form "logic table" from 000 to 111
conditions = [
~df.A & ~df.B & ~df.C,
~df.A & ~df.B & df.C,
~df.A & df.B & ~df.C,
~df.A & df.B & df.C,
df.A & ~df.B & ~df.C,
df.A & ~df.B & df.C,
df.A & df.B & ~df.C,
df.A & df.B & df.C,
]
chooses = [
"",
"Text3;",
"Text2;",
"Text2;Text3;",
"Text1;",
"Text1;Text3;",
"Text1;Text2;",
"Text1;Text2;Text3;",
]
df["E"] = np.select(conditions,chooses)
df
is the same as above now.
Upvotes: 2