BRUNOCAMARGOF
BRUNOCAMARGOF

Reputation: 11

Use part of string in DF

Please a need to return a part of string

I have this (example):

df = pd.DataFrame({'vals': [1, 2, 3, 4], 'ids': ['XXX2100M', 'yyyy2100M', 'AAA850M', 
    'BBB2100M']})

My goal:

  vals        ids    test
0    1   XXX2100M   2100M
1    2  yyyy2100M   2100M
2    3    AAA850M   
3    4      2100M   2100M

Modify ['test'] just if i have '2100M' on string.

Upvotes: 1

Views: 32

Answers (2)

Umar.H
Umar.H

Reputation: 23099

using str.extract which accepts a regular expression as an argument

df['test'] = df['ids'].str.extract('(2100M)').fillna('')

print(df)

   vals        ids   test
0     1   XXX2100M  2100M
1     2  yyyy2100M  2100M
2     3    AAA850M       
3     4   BBB2100M  2100M

Upvotes: 1

yatu
yatu

Reputation: 88305

We can use np.where with str.contains:

import numpy as np

df['test'] = np.where(df.ids.str.contains('2100M'), '2100M', '')

print(df)

   vals        ids   test
0     1   XXX2100M  2100M
1     2  yyyy2100M  2100M
2     3    AAA850M       
3     4   BBB2100M  2100M

Upvotes: 2

Related Questions