python - Replace values in one column without NA -


i've dataframe want replace values in 1 column. column:

         col1 0         0.0 1 -89999991.0 2         1.0 3         2.0 4         0.0 5        11.0 

i want replace values <0 0 in col1 using df.loc[df.col1 <0, 'col2'] = 0and write them col2:

   col1 0   0.0 1   0.0 2   1.0 3   2.0 4   0.0 5  11.0 

but problem replaced values nas:

  col2 0   na 1  0.0 2   na 3   na 4   na 5   na 

and don't understand problem.

need mask replace condition:

df['col2'] = df['col1'].mask(df.col1 <0, 0) 

or numpy.where:

df['col2'] = np.where(df.col1 <0, 0, df['col1']) print (df)          col1  col2 0         0.0   0.0 1 -89999991.0   0.0 2         1.0   1.0 3         2.0   2.0 4         0.0   0.0 5        11.0  11.0 

another solution clip_lower:

df['col2'] = df['col1'].clip_lower(0) print (df)          col1  col2 0         0.0   0.0 1 -89999991.0   0.0 2         1.0   1.0 3         2.0   2.0 4         0.0   0.0 5        11.0  11.0 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -