Conditional Replace Pandas
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Switch On Looping
--
Chapters
00:00 Question
00:38 Accepted answer (Score 269)
01:49 Answer 2 (Score 102)
02:09 Answer 3 (Score 74)
02:29 Answer 4 (Score 34)
03:46 Thank you
--
Full question
https://stackoverflow.com/questions/2160...
Accepted answer links:
[deprecated]: http://pandas.pydata.org/pandas-docs/sta...
Answer 2 links:
[has been deprecated]: https://pandas.pydata.org/pandas-docs/st...
Answer 3 links:
[np.where]: https://docs.scipy.org/doc/numpy/referen...
Answer 4 links:
[chained indexing]: https://pandas.pydata.org/pandas-docs/st...
[docs]: https://pandas.pydata.org/pandas-docs/st...
[loc]: https://pandas.pydata.org/pandas-docs/st...
[mask]: https://pandas.pydata.org/pandas-docs/st...
[np.where]: https://docs.scipy.org/doc/numpy/referen...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas #replace #conditionalstatements #series
#avk47
ACCEPTED ANSWER
Score 279
.ix indexer works okay for pandas version prior to 0.20.0, but since pandas 0.20.0, the .ix indexer is deprecated, so you should avoid using it. Instead, you can use .loc or iloc indexers. You can solve this problem by:
mask = df.my_channel > 20000
column_name = 'my_channel'
df.loc[mask, column_name] = 0
Or, in one line,
df.loc[df.my_channel > 20000, 'my_channel'] = 0
mask helps you to select the rows in which df.my_channel > 20000 is True, while df.loc[mask, column_name] = 0 sets the value 0 to the selected rows where maskholds in the column which name is column_name.
Update:
In this case, you should use loc because if you use iloc, you will get a NotImplementedError telling you that iLocation based boolean indexing on an integer type is not available.
ANSWER 2
Score 102
Try
df.loc[df.my_channel > 20000, 'my_channel'] = 0
Note: Since v0.20.0, ix has been deprecated in favour of loc / iloc.
ANSWER 3
Score 79
np.where function works as follows:
df['X'] = np.where(df['Y']>=50, 'yes', 'no')
In your case you would want:
import numpy as np
df['my_channel'] = np.where(df.my_channel > 20000, 0, df.my_channel)
ANSWER 4
Score 36
The reason your original dataframe does not update is because chained indexing may cause you to modify a copy rather than a view of your dataframe. The docs give this advice:
When setting values in a pandas object, care must be taken to avoid what is called chained indexing.
You have a few alternatives:-
loc + Boolean indexing
loc may be used for setting values and supports Boolean masks:
df.loc[df['my_channel'] > 20000, 'my_channel'] = 0
mask + Boolean indexing
You can assign to your series:
df['my_channel'] = df['my_channel'].mask(df['my_channel'] > 20000, 0)
Or you can update your series in place:
df['my_channel'].mask(df['my_channel'] > 20000, 0, inplace=True)
np.where + Boolean indexing
You can use NumPy by assigning your original series when your condition is not satisfied; however, the first two solutions are cleaner since they explicitly change only specified values.
df['my_channel'] = np.where(df['my_channel'] > 20000, 0, df['my_channel'])