I have a df,
A B
0 a 1.0
1 b,c 2.0
2
3 c,d NaN
I am trying to slice df["A"] when df["B"] is NaN or empty space " "
, I failed in slicing it by (,) Please help me to achieve it, my desired output is,
out_df,
A B
0 a 1.0
1 b,c 2.0
2
3 c NaN
Use np.where
i.e
df['new'] = np.where((pd.isnull(df['B']))|(df['B']==''), df['A'].str.split(',').str[0], df['A'])
or df.where
i.e
df['A'].where((pd.notnull(df['B']))|(df['B']==''), df['A'].str.split(',').str[0])
A B new 0 a 1 a 1 b,c 2 b,c 2 3 c,d NaN c