Lynn
Lynn

Reputation: 4398

Separate several columns of data that contain hyphens , removing elements in Python

I have a dataset, df, where I would like to separate strings within Python.

Data

 Type                 Id        
 aa - generation      aa - generation01 
 aa_led - generation  aa_led - generation01
 ss - generation      ss- generation01  

Desired

Type    Id
aa      aa01
aa_led  aa_led01
ss      ss01

Doing

I am trying to incorporate this code into my script, however, this splits by hyphen but my column names are not reserved.

new = wordstring.strip('-').split('-')

Any suggestion is appreciated Thank you

Upvotes: 0

Views: 107

Answers (1)

Nk03
Nk03

Reputation: 14949

If you just want to remove generation from every value in df. You can use applymap:

df = df.applymap(lambda x : x.replace('- generation', '').replace(' ',''))

OUTPUT:

     Type        Id        
0      aa       aa01
1  aa_led   aa_led01
2      ss       ss01

Upvotes: 1

Related Questions