Paras
Paras

Reputation: 17

How to get a substring between two substrings in pandas?

I Have a column in Dataframe in which the data is as below.

Invoice Number
Being InvoiceNC/0095/2022.23 Dt. 06.10.2022
Black Toner Cartridge agst InvoiceNC/0098/2022.23 dt 10.10.2022
Being amount vide InvoiceS0001236 Dt. 03.10.2022
Being amount vide InvoiceS0001235 Dt. 03.10.2022

I'd like to remove everything from the column but the string between 'Invoice' and next " ".

Any help will be appretiated.

Upvotes: 0

Views: 239

Answers (2)

phœnix
phœnix

Reputation: 367

you can use :

df['Invoice Number']=a
l=[]
k=[]
for i in range(len(a)):
    l.append(a[i].split('Invoice')[1])
    k.append(l[i].split(' ')[0])

df['Invoice Number']=k# replace  in df by new values

Upvotes: 0

Panda Kim
Panda Kim

Reputation: 13267

s = df['Invoice Number'].str.extract('Invoice(\S+)')

s

    0
0   NC/0095/2022.23
1   NC/0098/2022.23
2   S0001236
3   S0001235

Upvotes: 1

Related Questions