2

I have a dataframe with a column like that

COL1
PACK[30% /2 prod.(if fidelity)]
PACK[3.85 € /2 prod.(if fidelity)]
PACK[40% /2nd prod.]
PACK[3.5 € /2 prod.]

I want create other column as following according COL1

fidelity_perc fidelity_euro rem_perc  rem_eu
30           3,85           40        3,5

using regex.

For PACK[40% /2nd prod.] I did (?<=PACK\[)\d+(?=%) but it's also walk for PACK[30% /2 prod.(if fidelity)] and I don't want this.

9
  • Maybe you want df['COL1'].str.extract(r'PACK\[(\d[\d.]*)%[^][()]*]', expand=False)? See regex101.com/r/3jROEh/2 Commented Dec 2, 2020 at 14:44
  • @Wiktor Thank you, and I want to add contains "fidelity" in my regex for example how could I do ? Commented Dec 2, 2020 at 14:51
  • df['fidelity_perc'] = df['COL1'].str.extract(r'PACK\[(\d[\d.]*)%[^][()]*]', expand=False)? Commented Dec 2, 2020 at 14:53
  • @Wiktor This is ok for PACK[40% /2nd prod.] but not for PACK[30% /2 prod.(if fidelity)], I have to speficy contains "fidelity" Commented Dec 2, 2020 at 14:55
  • @viktor, it's not ok for fidelity I am trying Commented Dec 2, 2020 at 15:04

1 Answer 1

2

To get the PACK[40% /2nd prod.], you can use

df['COL1'].str.extract(r'PACK\[(\d[\d.]*)%[^][()]*]', expand=False)

See this regex demo.

To get the PACK[30% /2 prod.(if fidelity)], you can use

df['COL1'].str.extract(r'PACK\[(\d[\d.]*)%[^][]*fidelity[^][]*]', expand=False)

See this regex demo

Similarly, if you need to only find a value if there is a + sign, just replace fidelity with \+:

df['COL1'].str.extract(r'PACK\[(\d[\d.]*)%[^][]*\+[^][]*]', expand=False)

Details

  • PACK\[ - a literal PACK[ string
  • (\d[\d.]*) - Capturing group 1: a digit an then any zero or more digits or dots as many as possible
  • % - a % sign
  • [^][]* - any zero or more chars other than [ and ] as many as possible
  • \+ - a plus
  • fidelity - a fidelity substring
  • [^][]* - any zero or more chars other than [ and ] as many as possible
  • [^][()]* - any zero or more chars other than [, ], ( and ) as many as possible
  • ] - a ] char.
Sign up to request clarification or add additional context in comments.

4 Comments

Hello I have an other question ! if want to add "not contains +" how could I do?
@T.mum You add + into the negated character class.
what's the negated class?
@T.mum See here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.