ah bon
ah bon

Reputation: 10011

Remove part of string after a specific character in Python

Given a time column as follows:

             time
0   2019Y8m16d10h
1    2019Y9m3d10h
2  2019Y9m3d10h58s
3    2019Y9m3d10h

How can I remove substrings start by d, I have tried with df['time'].str.split('d')[0], but it doesn't work.

My desired result will like this. Thank you.

        time
0    2019Y8m16d
1    2019Y9m3d
2    2019Y9m3d
3    2019Y9m3d

Upvotes: 1

Views: 128

Answers (3)

Raghul Raj
Raghul Raj

Reputation: 1458

Or you can simply use apply functionality to solve the purpose as follows:

df.apply(lambda x: x['time'].split('d')[0]+'d',axis=1)

Upvotes: 1

jezrael
jezrael

Reputation: 862611

You are close, need str[0] for select lists and then add d:

df['time'] = df['time'].str.split('d').str[0].add('d')

Or:

df['time'] = df['time'].str.split('(d)').str[:2].str.join('')

print (df)
         time
0  2019Y8m16d
1   2019Y9m3d
2   2019Y9m3d
3   2019Y9m3d

Or use Series.str.extract:

df['time'] = df['time'].str.extract('(.+d)')
print (df)
         time
0  2019Y8m16d
1   2019Y9m3d
2   2019Y9m3d
3   2019Y9m3d

Upvotes: 1

Valdi_Bo
Valdi_Bo

Reputation: 30971

One of possible solutions:

df['time'].str.extract(r'([^d]+d)')

Upvotes: 1

Related Questions