1

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

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this works but d get missing as well and I want to keep it.
1

One of possible solutions:

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

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.