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
.
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