Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas replace non-zero values

Tags:

python

pandas

I know I can replace all nan values with df.fillna(0) and replace a single value with df.replace('-',1), but how can I replace all non-zero values with a single value?

like image 773
pyCthon Avatar asked Jun 03 '14 17:06

pyCthon


People also ask

How do I change the values in pandas series based on conditions?

You can replace values of all or selected columns based on the condition of pandas DataFrame by using DataFrame. loc[ ] property. The loc[] is used to access a group of rows and columns by label(s) or a boolean array. It can access and can also manipulate the values of pandas DataFrame.


1 Answers

Use boolean indexing:

df[df != 0] = value
like image 67
TomAugspurger Avatar answered Oct 13 '22 01:10

TomAugspurger