Extract a number from a string after a specific character

I have a dataframe (~ 1 million rows) with a column ("Product") that contains rows like "none", "q1", "q123" or "q12_a123".

I would like to extract the number that follows the letter “q” and enter it in another column (“Total Amount”) so that it looks like this:

'Product'    'AmountPaid'
 none            0
 q1              1
 q123            123
 q12_a123        12

So far I have:

for i in range(0,1000000):
   if 'q' not in df.loc[i,'Product']:
      df.loc[i,'AmountPaid']=0
   else:
      # set 'AmountPaid' to the number following 'q'

Questions:

  • How to extract the number (number) immediately after the letter "q", but not necessarily everything after it? For example, extract 12 from "q12_a123".
  • Most AmountPaid entries will be set to 0. Is there a more efficient way than the for and if / else loop above?
+4
source share
1

str.extract lookbehind 'q'.

df['AmountPaid'] = df.Product.str.extract(
      r'(?<=q)(\d+)', expand=False
).fillna(0).astype(int)

df

    Product  AmountPaid
0      none           0
1        q1           1
2      q123         123
3  q12_a123          12
+5

Source: https://habr.com/ru/post/1695264/


All Articles