Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Question on conditional syntax for new col in pandas df

$
0
0

I'm struggling to create a new col in a df based on an existing column using a condition. Essentially if the Client Contract Number contains an underscore, I want the value in the new column to be all characters before the underscore, otherwise I want it to be the Client Contract Number with all dashes removed. I'm able to remove the dashes with the below, but the second line doesn't work

raw_data_df['Search Text'] = raw_data_df['Client Contract Number'].str.replace('-','')raw_data_df['Search Text'] = raw_data_df['Client Contract Number'].str.split('_')[0] if raw_data_df['Client Contract Number'].str.contains("_") else raw_data_df['Client Contract Number'].str.replace('-','')

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>