Home > Enterprise >  Python column entries selection
Python column entries selection

Time:10-10

How can we can simply tranform the values in column 'colors' into column 'new_colors' in python as indicated below:

    colors        new_colors 
0   red          red        
1   blue;yellow  blue  
2   red;green    red

Do we need apply split method to 'colors' column or is there a simpler way of approaching this so that 'new_colors' only show the first color in 'colors'.

Thanks in advance!

CodePudding user response:

Try str.split:

df['new_colors'] = df['colors'].str.split(';').str[0]

Or try:

df['new_colors'] = df['colors'].str.replace(';.*', '')
  • Related