Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Pandas series containing string to boolean

I have a DataFrame named df as

  Order Number       Status 1         1668  Undelivered 2        19771  Undelivered 3    100032108  Undelivered 4         2229    Delivered 5        00056  Undelivered 

I would like to convert the Status column to boolean (True when Status is Delivered and False when Status is Undelivered) but if Status is neither 'Undelivered' neither 'Delivered' it should be considered as NotANumber or something like that.

I would like to use a dict

d = {   'Delivered': True,   'Undelivered': False } 

so I could easily add other string which could be either considered as True or False.

like image 215
working4coins Avatar asked Jul 17 '13 14:07

working4coins


1 Answers

You can just use map:

In [7]: df = pd.DataFrame({'Status':['Delivered', 'Delivered', 'Undelivered',                                      'SomethingElse']})  In [8]: df Out[8]:           Status 0      Delivered 1      Delivered 2    Undelivered 3  SomethingElse  In [9]: d = {'Delivered': True, 'Undelivered': False}  In [10]: df['Status'].map(d) Out[10]: 0     True 1     True 2    False 3      NaN Name: Status, dtype: object 
like image 187
joris Avatar answered Oct 09 '22 06:10

joris