max - 3 months ago 68

Python Question

`np.where`

`when`

`otherwise`

`np.where`

`Series`

`pandas`

`numpy`

`pd.Series`

`pd.DataFrame`

Sure enough, I found

`pandas.DataFrame.where`

`np.where`

`where`

`# df is pd.DataFrame`

# how to write this using df.where?

df['C'] = np.where((df['A']<0) | (df['B']>0), df['A']+df['B'], df['A']/df['B'])

Am I missing something obvious? Or is pandas

`where`

`np.where`

Answer

Try:

```
(df['A'] + df['B']).where((df['A'] < 0) | (df['B'] > 0), df['A'] / df['B'])
```

The difference between the `numpy`

`where`

and `DataFrame`

`where`

is that the default values are supplied by the `DataFrame`

that the `where`

method is being called on (docs).

I.e.

```
np.where(m, A, B)
```

is roughly equivalent to

```
A.where(m, B)
```

If you wanted a similar call signature using pandas, you could take advantage of the way method calls work in Python:

```
pd.DataFrame.where(cond=(df['A'] < 0) | (df['B'] > 0), self=df['A'] + df['B'], other=df['A'] / df['B'])
```

or without kwargs (Note: that the positional order of arguments is different from the `numpy`

`where`

argument order):

```
pd.DataFrame.where(df['A'] + df['B'], (df['A'] < 0) | (df['B'] > 0), df['A'] / df['B'])
```

Source (Stackoverflow)

Comments