A simple way to distinguish between 0 and False in a mixed-value data frame

I have a column in my data frame where the values ​​are either 1, 0, False , but rows with False or O are functionally different.

So I would like to convert the values False or 0 to something else

What would be a good way to do this?

Using a replacement did not work.

df["col_name"] = df["col_name"].replace(0,2) also converts False values

and

df["col_name"] = df["col_name"].replace(False,2) also converts the values 0

+5
source share
4 answers

You can use mask to replace values ​​with boolean mask - the advantage of this solution is there is no original types :

 df = pd.DataFrame({'Col':[1, False, 0]}) df['Col'] = df['Col'].mask(df['Col'].astype(str) == '0', 2).replace(False, 3) print (df) Col 0 1 1 3 2 2 

The solution is with a Series.replace dict , but first converting to str on astype works, but usually it converts all the values ​​to str , which can be a problem with real data.

 d = {'0':'Zero', 'False':False} df = df['Col'].astype(str).replace(d) print (df) 0 1 1 False 2 Zero Name: Col, dtype: object 

I will try to create a more general solution with map and check bools on isinstance :

 df = pd.DataFrame({'Col':[1, False, 0, True,5]}) print (df) Col 0 1 1 False 2 0 3 True 4 5 m = df['Col'].apply(lambda x: isinstance(x, bool)) df['Col'] = df['Col'].mask(m, df['Col'].map({False:2, True:3})) print (df) Col 0 1 1 2 2 0 3 3 4 5 
+6
source

Use astype :

 df = pd.DataFrame({'Datavalue':[1,False,0]}) df.Datavalue.astype(str) == "0" 

Output:

 0 False 1 False 2 True Name: Datavalue, dtype: bool df.loc[df.Datavalue.astype(str) == "0",'Datavalue'] = "Zero" 

Output:

  Datavalue 0 1 1 False 2 Zero 
+4
source

You can convert to str type and then use df.str.replace :

 In [223]: df = pd.DataFrame({'Col':[1, False, 0]}) In [224]: df.Col.astype(str).replace('0', 'Zero').replace('False', np.nan) Out[224]: 0 1 1 NaN 2 Zero 
+4
source

Use jezrael dataframe df

 df = pd.DataFrame({'Col':[1, False, 0]}) 

Option 1
If there are only three values: 1, 0 or False, then the type of the bool type is no worse than False

 df.Col.mask(df.Col.apply(type) == bool, 2) 0 1 1 2 2 0 Name: Col, dtype: object 

Option 2
You can use the python is operator

 False is 0 False 

And use mask as it was before

 df.mask(df.Col.apply(lambda x: x is False), 2) 0 1 1 2 2 0 Name: Col, dtype: object 
+3
source

Source: https://habr.com/ru/post/1270043/


All Articles