Home > Software engineering >  Python: change column of strings with None to 0/1
Python: change column of strings with None to 0/1

Time:03-11

My dataframe is

df = pd.DataFrame({"label":['a', 'b', None, 'c', None]})

and I wish to change it such that all None are set to 0 and everything else is set to 1. I used df.iterrows() but it really feels barbaric and seems to be slow.

What would be an efficient, pythonic way?

CodePudding user response:

The following worked for me :

df = pd.DataFrame({"label":['a', 'b', None, 'c', None]})
print(df)
print('-------------------')
df = df.fillna(value=0)
print(df)

You can read more about it here

CodePudding user response:

Try one of the following

df.label.notna().mul(1)
df.label.map({None: 0}).fillna(1).astype(int)
import numpy as np
np.where(df.label.isna(), 0, 1)

CodePudding user response:

You can use if_else from datar:

>>> import pandas as pd
>>> from datar.all import f, mutate, if_else, is_na
>>> df = pd.DataFrame({"label":['a', 'b', None, 'c', None]})
>>> df >> mutate(if_else(is_na(f.label), 0, 1))
     label         x
  <object> <float64>
0        a       1.0
1        b       1.0
2     None       0.0
3        c       1.0
4     None       0.0
>>> # or
>>> if_else(is_na(df.label), 0, 1)
0    1.0
1    1.0
2    0.0
3    1.0
4    0.0
Name: x, dtype: float64
  • Related