Unfortunately there is no sense of unity in the where method

I felt that the behavior of where of numpy and pandas was contrary, so I will share it.

There are where and mask methods that assign values depending on the condition. Details of the method can be found at here. (I am always indebted to you: pray :)

Here's a good example.

import numpy as np
import pandas as py

test = np.array([1,2,3])

Suppose you want to replace this 2 with 200.

Let's do it with np.where.

>>> pd.Series(np.where(test == 2, 200, test))
0      1
1    200
2      3
dtype: int64

You will get the desired result.

So what about pd.where?

>>> pd.Series(test).where(test == 2, 200)
0    200
1      2
2    200
dtype: int64

I'm sorry. The result was the opposite of what I was aiming for ...

Use pd.mask instead of np.where.

>>> pd.Series(np.where(test == 2, 200, test))
0      1
1    200
2      3
dtype: int64

I want you to match the behavior with np.where ...

Recommended Posts

Unfortunately there is no sense of unity in the where method
Get the class name where the method is defined in the decorator
There is no switch in python
Is there NaN in the pandas DataFrame?
In Python, change the behavior of the method depending on how it is called
Around the place where the value of Errbot is stored
If there were no DI containers in the world.
What is the true identity of Python's sort method "sort"? ??
The copy method of pandas.DataFrame is deep copy by default
Automatically get the port where Arduino is stuck in Python
Is there a bias in the numbers that appear in the Fibonacci numbers?
Numerical approximation method when the calculation of the derivative is troublesome
Is there a secret to the frequency of pi numbers?
Let's run Fusion 360 with Python Part 11 Since there is no point along the path in the API, I thought of an alternative
When merging via pull request, there is no committer information in the response from Github API
Sequential processing method when there is not enough memory in Keras
The hot battle of professional wrestling is illustrated in UML (overview)
A function that measures the processing time of a method in python
[Linux] What is the method to solve the package dependency error of yum and rpm in the actual workplace?
The story of participating in AtCoder
Is the probability of precipitation correct?
Is there a special in scipy? ??
The story of the "hole" in the file
The meaning of ".object" in Django
Science "Is Saito the representative of Saito?"
The google search console sitemap api client is in webmasters instead of search console
What kind of book is the best-selling "Python Crash Course" in the world?
Find out the name of the method that called it from the method that is python
[Introduction to Python] What is the method of repeating with the continue statement?