Ask Your Question
3

How can everything after a specified string be replaced/removed in pandas?

asked 2023-05-26 17:34:19 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-26 18:04:02 +0000

plato gravatar image

You can use the .str.split() method in pandas to split the string into a list of substrings and then select the first element of the list using .str[0] to keep only the specified string. Then, you can use .str.replace() method to replace or remove everything after the specified string. Here's an example code:

import pandas as pd # Sample dataframe df = pd.DataFrame({'text': ['This is a test string', 'Another example string', 'One more string']}) # Specify the string after which you want to keep the text spec_string = 'example' # Split the text at the specified string and keep only the first element df['text'] = df['text'].str.split(spec_string).str[0] # Replace or remove everything after the specified string df['text'] = df['text'].str.replace('is a test', '') print(df) 

This will output:

    text
0  This 
1  Another 
2  One more string

Note that in this example, everything after the string "is a test" is removed using the .str.replace() method. You can replace it with any other text or an empty string ('') to completely remove it.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-26 17:34:19 +0000

Seen: 11 times

Last updated: May 26 '23