Reputation: 11
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
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
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