简体   繁体   中英

How to convert date format string to bool True and others to False?

import pandas
import numpy
df=pandas.DataFrame({'col1':['a','b','c','b'],'col2':['N','2018-03-12 15:35',numpy.NaN,'2017-06-12 15:35'],'col3':['c','b','b','b']})
print(df)

Output of above script is:

  col1              col2 col3
0    a                 N    c
1    b  2018-03-12 15:35    b
2    c               NaN    b
3    b  2017-06-12 15:35    b

As to column col2 ,I want to convert all yyyy-mm-dd hh:mm format string to bool True ,others to False ,keep Na value as same.
The expect result as below:

  col1              col2 col3
0    a             False    c
1    b              True    b
2    c               NaN    b
3    b              True    b

How to do it? Thanks in advance!

Create 2 masks - first convert to_datetime s with errors='coerce' and test Series.notna and also test this column:

m1 = pd.to_datetime(df['col2'], errors='coerce').notna()
m2 = df['col2'].notna()

Then pass it to numpy.select - but is necessary convert NaN to None :

df['col2'] = np.select([m1, m2], [True, False], None)
print(df)
0    a  False    c
1    b   True    b
2    c   None    b
3    b   True    b

Or use DataFrame.loc :

df.loc[m2, 'col2'] = m1
print(df)
  col1   col2 col3
0    a  False    c
1    b   True    b
2    c    NaN    b
3    b   True    b

You can use something like:

m=df.col2.notna()
df.loc[m,'col2']=(pd.to_datetime(df.col2.dropna(),errors='coerce').isna()
                                            .map({True:False,False:True}))
    print(df)
  col1   col2 col3
0    a  False    c
1    b   True    b
2    c    NaN    b
3    b   True    b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM