Home > Back-end >  Can I get the numbers after the underscore?
Can I get the numbers after the underscore?

Time:05-04

Input values

id  values
0   Q56_0.01 Q57_0.99
1   Q1_0.01 Q57_0.96 Q67_0.03

I need the output dataframe in the below format

Output

id  values
0   0.01 
0   0.99
1   0.01 
1   0.96 
1   0.03

CodePudding user response:

It would be better to explain your question with the code but as far as I understand answer of your question should be like that: I used split function -> https://www.w3schools.com/python/ref_string_split.asp

my_string="Q56_0.01"
print (my_string.split("_",1)[1]) 

Output : 0.01

CodePudding user response:

Assuming a dataframe, you can use str.findall combnied with explode:

(df
 .assign(values=df['values'].str.findall('_(\d (?:\.\d ))'))
 .explode('values')
 .astype({'values': float})
)

output:

   id  values
0   0    0.01
0   0    0.99
1   1    0.01
1   1    0.96
1   1    0.03
  • Related