Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

From a string with repeated under scores (e.g. 1_2_3_4_5_6), split and select 3_4

The header of my data frame looks like this

header = list(data_no_control.columns.values)
header

['MLID_D_08_NGS_34_H08.fsa',
 'MLID_D_25_NGS_38_A11.fsa',
 'MLID_D_36_NGS_41_D12.fsa',
 'MLID_D_37_NGS_42_E12.fsa']

I want to change my header to look like this

['NGS_34',
 'NGS_38',
 'NGS_41',
 'NGS_42']

How can I do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

header = ['MLID_D_08_NGS_34_H08.fsa',
 'MLID_D_25_NGS_38_A11.fsa',
 'MLID_D_36_NGS_41_D12.fsa',
 'MLID_D_37_NGS_42_E12.fsa']

new_header = []

for item in header:
    item = item.split('_')
    new_header.append(item[3] + '_' + item[4])

# output: ['NGS_34', 'NGS_38', 'NGS_41', 'NGS_42']
print(new_header)  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading