Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert dataframe of booleans to dataframe of 1 and np.NaN?

Tags:

pandas

I have a dataframe filled with True and False values, and I'd like to get a dataframe from it with the True replaced with 1 and the False replaced with np.NaN. I've tried using dataframe.replace, but it gave a dataframe filled with all True. Is there a way to do it without using for loops and if's?

Example, this is the dataframe I have, with T for True and F for False (not strings 'T' and 'F'; sorry, could not figure out how to format a nicely spaced table in the wiki):

2008-01-02 16:00:00 T T F
2008-01-03 16:00:00 T T T
2008-01-04 16:00:00 T T F
2008-01-07 16:00:00 T T T
2008-01-08 16:00:00 T T F

This is what I would like to change it to:

2008-01-02 16:00:00 1 1 np.NaN
2008-01-03 16:00:00 1 1 1
2008-01-04 16:00:00 1 1 np.NaN
2008-01-07 16:00:00 1 1 1
2008-01-08 16:00:00 1 1 np.NaN

These are the lines I tried to replace the True and False, and got a dataframe filled with all True values:

df.replace(to_replace=True, value=1, inplace=True, method=None)   
df.replace(to_replace=False, value=np.NAN, inplace=True, method=None)  

When tried separately, the first line alone does not change anything; the second line converts all the values to True.

like image 259
d l Avatar asked Dec 26 '22 11:12

d l


1 Answers

applymap() can be used to apply a function to every element of a dataframe

In [1]: df = DataFrame([[True, True, False],[False, False, True]]).T

In [2]: df
Out[2]:
       0      1
0   True  False
1   True  False
2  False   True

In [3]: df.applymap(lambda x: 1 if x else np.nan)
Out[3]:
    0   1
0   1 NaN
1   1 NaN
2 NaN   1

You can also use a dict:

In [4]: d = {True:1, False:np.nan}

In [5]: df.applymap(lambda x: d[x])
Out[5]:
    0   1
0   1 NaN
1   1 NaN
2 NaN   1

Addressing DSM's comment from below. I misread the OP and assumed the datetime was an index. If it's not an index this worked for me:

In [6]: df.applymap(lambda x: d.get(x,x))
Out[6]:
    0   1                    2
0   1 NaN  2012-01-01 00:00:00
1 NaN   1  2012-01-01 00:00:00
like image 158
Zelazny7 Avatar answered Jun 17 '23 09:06

Zelazny7