Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

4
  • 1
    Related: Create new column based on values from other columns / apply a function of multiple columns, row-wise in Pandas (same idea, but the selection criteria are based on multiple columns)
    – wjandrea
    Commented Sep 24, 2023 at 23:11
  • 1
    Since pandas 2.2.0, you can use case_when (a syntax useful in SQL in the context in the OP). See this answer for a demo.
    – cottontail
    Commented Mar 10 at 12:11
  • @wjandrea if you insist that the distinction between doing this for a single column and for multiple columns is important, then this question needs to be clear that it's only about the single-column case - so I edited the title accordingly. Commented Jun 8 at 18:26
  • @KarlKnechtel Reading this back now, the more important thing is that there's only one criterion, which lets you use np.where instead of having to use np.select. I guess I misphrased my comment.
    – wjandrea
    Commented Jun 8 at 18:37