Cleansing panda dataframe using regular expression

Viewed 42

I have a panda data frame that I need to cleanse one of the columns. The column has few possible values. For example, if it contains LC50 =1.4±0.2μM I need to take following actions:

  1. find LC50 =
  2. if it find the LC50 =, need to take the number before ± (i.e., 1.4)
  3. if it finds μM, mM,µg/ml, or etc. scales, remove from this column and add it to a new column that I have added before.
  • How to do this?
  • Is there any resources that I can learn more specifically and practically about regular expression and data cleansing using them using sample codes? if yes, please share.
1 Answers

I would go with:

df['col'].str.extract(r'LC50\s*=\s*(\d+(?:\.\d+)?)[±\d.\s]*(μM|mM|µg/ml)')

Example input:

               col
0  LC50 =1.4±0.2μM

Output:

     0   1
0  1.4  μM

regex:

LC50\s*=\s*      # LC50= with optional spaces
(\d+(?:\.\d+)?)  # captured number with optional decimal part
[±\d.\s]*        # intermediate "junk" (±/dot/space/digit)
(μM|mM|µg/ml)    # captured unit (from a list of possibilities)
Related