I was trying to use pandas.where() to remove negative values from a column in a data frame.
The most obvious way to remove the negative values is to simply run pandas.abs() on the column. So:
import pandas as pd
frame = pd.DataFrame([-1,-1,-3,-4,-5],columns=["amount"])
frame.amount = frame.amount.abs()
But I wanted to try the same thing using pandas.where(). So I tried the following:
frame.amount = frame["amount"].where(frame["amount"] < 0, frame["amount"].abs(), inplace=True)
Which returns:
Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
amount
0 None
1 None
2 None
3 None
4 None
Two things confused me:
- I was surprised I had to assign each expression (frame.amount = ...) because I thought calling the operation in either case would mutate the Dataframe (isn't that what 'inplace' should do?) and
- why does pandas.where() return 'None'