ColA ColB
0 Vendor account : TX-8888881
1 Invoice account : sdfhsa
2 NaN NaN
3 2020-11-01 00:00:00 NaN
4 Vendor account : TX-8888885
我有两列,如上所述,我想创建一个新列,如果"ColA"中有值"Vendor account:",则该列的值来自列"ColB",否则为Nan。
所需表格如下。
ColA ColB VendorAccount
0 Vendor account : TX-8888881 TX-8888881
1 Invoice account : sdfhsa
2 NaN NaN
3 2020-11-01 00:00:00 NaN
4 Vendor account : TX-8888885 TX-8888885
我正在使用代码df['VendorAcount']= np.where(df['ColA'] == 'Vendor account :' , df['ColB'] , np.nan)
我认为您需要通过Series.str.startswith
:测试子字符串
df['VendorAcount']= np.where(df['ColA'].str.startswith('Vendor account'),df['ColB'], np.nan)
或通过.astype(str)
:开始字符串
df['VendorAcount']= np.where(df['ColA'].astype(str) == 'Vendor account :' , df['ColB'] , np.nan)
您也可以尝试将值转换为CCD_5
PD_6