This is actually the next question here . I was not clear in my previous question, and since he received the answer, I felt it was better to put a new question instead.
I have a dataframe as shown below:
Column1 Column2 Column3 Column4 Column5
5FQ 1.047 S$55.3 UG44.2 as of 02/Jun/2016 S$8.2 mm
600 (1.047) S$23.3 AG5.6 as of 02/Jun/2016 S$58 mm
KI2 1.695 S$5.35 RR59.5 as of 02/Jun/2016 S$705 mm
88G 0.0025 S$(5.3) NW44.2 as of 02/Jun/2016 S$112 mm
60G 5.63 S$78.4 UG21.2 as of 02/Jun/2016 S$6.21 mm
90F (5.562) S$(88.3) IG46.2 as of 02/Jun/2016 S$8 mm
I am trying to use regexto delete all words and letters, keeping only numbers. However, if the number is within (), I would like to make a negative number instead.
Desired Conclusion
Column1 Column2 Column3 Column4 Column5
5 1.047 55.3 44.2 8.2
600 -1.047 23.3 5.6 58
2 1.695 5.35 59.5 705
88 0.0025 -5.3 44.2 112
60 5.63 78.4 21.2 6.21
90 -5.562 -88.3 46.2 8
Is it possible? I tried playing with this code, but did not know what the appropriate combination should be regex.
df.apply(lambda x: x.astype(str).str.extract(r'(\d+\.?\d*)', expand=True).astype(np.float))
source
share