Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace non Null values in column by 1

I have a dataframe that have a column that looks like:

note

129.0
130.0
131.0
132.0
133.0
134.0
135.0
136.0
137.0
138.0
139.0
140.0
141.0
142.0

143.0

So there are the rows that don't contain the values (NaN). I'de like to replace the numerical values that are not NaN by 1, to have:

note
1
1
1
1
1
1
1

1

I tried this code:

def replace():
    if  (pd.notnull(df['note'])):
        df['note'] = '1'
        return df
    return df

it returns me ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().


2 Answers

Use loc for this:

In [86]:
df.loc[df['note'].notnull(), 'note'] = 1
df

Out[86]:
    note
0      1
1      1
2      1
3      1
4      1
5      1
6      1
7      1
8      1
9      1
10     1
11     1
12     1
13     1
14     1

if (pd.notnull(df['note'])) won't work because if doesn't understand how to treat an array of boolean values hence the ValueError because you may have all -1 or only one True value in the boolean array

like image 143
EdChum Avatar answered Sep 10 '25 23:09

EdChum


You can also use where to achieve this as in an in-place operation. It will set any values not matching a predicate to a specified value.

import pandas as pd
import numpy as np

df = pd.DataFrame({'a': ['a', 'b', 'c'], 'note': [145.0, np.NaN, 147.0]})
df.note.where(df.note.isnull(), 1, inplace=True)

Yields

In [14]: print(df)
Out[14]: 
   a  note
0  a     1
1  b   NaN
2  c     1
like image 42
benjwadams Avatar answered Sep 10 '25 23:09

benjwadams