Making a new column in pandas based on conditions of other columns
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Secret Catacombs
--
Chapters
00:00 Question
01:09 Accepted answer (Score 2)
02:10 Thank you
--
Full question
https://stackoverflow.com/questions/4316...
Accepted answer links:
[eval]: http://pandas.pydata.org/pandas-docs/sta...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas #dataframe #lambda #apply
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Secret Catacombs
--
Chapters
00:00 Question
01:09 Accepted answer (Score 2)
02:10 Thank you
--
Full question
https://stackoverflow.com/questions/4316...
Accepted answer links:
[eval]: http://pandas.pydata.org/pandas-docs/sta...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas #dataframe #lambda #apply
#avk47
ACCEPTED ANSWER
Score 2
You can use eval here for short:
# create some dummy data
df = pd.DataFrame(np.random.randint(0, 10, size=(5, 2)),
columns=["col1", "col2"])
print(df)
col1 col2
0 1 7
1 2 3
2 4 6
3 2 5
4 5 4
df["col3"] = df.eval("col1 < 5 and col2 > 5")
print(df)
col1 col2 col3
0 1 7 True
1 2 3 False
2 4 6 True
3 2 5 False
4 5 4 False
You can also write it without eval via (df["col1"] < 5) & (df["col2"] > 5).
You may also enhance the example with np.where to explicitly set the values for the positive and negative cases right away:
df["col4"] = np.where(df.eval("col1 < 5 and col2 > 5"), "Positive Value", "Negative Value")
print(df)
col1 col2 col3 col4
0 1 7 True Positive Value
1 2 3 False Negative Value
2 4 6 True Positive Value
3 2 5 False Negative Value
4 5 4 False Negative Value