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:
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?
source
share