The Python Oracle

Making a new column in pandas based on conditions of other columns

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Civilization

--

Chapters
00:00 Making A New Column In Pandas Based On Conditions Of Other Columns
00:53 Accepted Answer Score 2
01:34 Thank you

--

Full question
https://stackoverflow.com/questions/4316...

--

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