'Can I get the numbers after the underscore?
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
Solution 1:[1]
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
Solution 2:[2]
Assuming a pandas 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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alasgar |
| Solution 2 | mozway |
