0

I have data that looks like this:

A    B    C    D
3    1    4    0.5
4    2    6    0.25
2    1    3   -0.4
3    3    6   -0.8

And I'd like to add a column E which is the result of either A divided by C or B divided by C based on if D < 0. I figured I should use apply in this situation but I don't really know how to work both a loop and an if statement into apply(). Any suggestions on how to tackle this?

Yoeril
  • 83
  • 6
  • 1
    When working with data you have to let go of the concept "loops", you have to think in vectors and methods which are made for these. Loops are extremely slow when iterating over data. `np.where` in the given answer is one of these vectorized methods. – Erfan Feb 19 '20 at 22:36
  • Thanks for help, I'll do some more research on how to iterate over dataframes. – Yoeril Feb 19 '20 at 22:40
  • No that's the thing, you **don't** want to iterate over dataframes.. – Erfan Feb 19 '20 at 22:57
  • 1
    Does this answer your question? [Pandas conditional creation of a series/dataframe column](https://stackoverflow.com/questions/19913659/pandas-conditional-creation-of-a-series-dataframe-column) – AMC Feb 19 '20 at 23:22
  • Read the Pandas docs. – AMC Feb 19 '20 at 23:22

1 Answers1

2

np.where might help here: Pandas conditional creation of a series/dataframe column

df['E'] = np.where(df['D'] < 0, df['A']/df['C'], df['B']/df['C'])
emiljoj
  • 399
  • 1
  • 7