1

I am trying to remove a string from a column using regular expressions and replace.

                      Name

"George @ ACkDk02gfe" sold

I want to remove " @ ACkDk02gfe"

I have tried several different variations of the code below, but I cant seem to remove string I want.

df['Name'] = df['Name'].str.replace('(\@\D+\"$)','')

The output should be

George sold

This portion of the string "ACkDk02gfe is entirely random.

5
  • What is up with the down voting the answers below without comment? Commented Jul 14, 2020 at 1:13
  • @ScottBoston They're all wrong and don't actually address the question; they show a lack of knowledge with the tools being used and what the question is. str.replace uses regex already; the problem is with the pattern OP has posted and I'm working on writing a correct answer now. Commented Jul 14, 2020 at 1:16
  • df['Name'].str.replace('"','').replace('@\s\w+\s','', regex=True) ? Commented Jul 14, 2020 at 1:17
  • @ScottBoston regex is True by default and you could combine the patterns into one, but yes, that would work as well as someone just copied your pattern into their answer 😂 Commented Jul 14, 2020 at 1:20
  • 1
    @MaximilianBurszley I know my answer isn't ideal as it is not RegEx, but it provides the correct output based off the input... and is not a horrible answer. I even prefaced my answer with "I'll let someone else post a regex answer, but this could also be done with split. I don't know how consistent the data you are looking at is, but this would work for the provided string:" Commented Jul 14, 2020 at 1:23

6 Answers 6

3

Let's try this using regex with | ("OR") and regex group:

df['Name'].str.replace('"|(\s@\s\w+)','', regex=True)

Output:

0    George sold
Name: Name, dtype: object

Updated

df['Name'].str.replace('"|(\s@\s\w*[-]?\w+)','')  

enter image description here

Where df,

                         Name
0  "George @ ACkDk02gfe" sold
1    "Mike @ AisBcIy-rW" sold

Output:

0    George sold
1      Mike sold
Name: Name, dtype: object
Sign up to request clarification or add additional context in comments.

2 Comments

@excelater -rW? I don't understand.
Sorry to clarify some of the rows has text like Mike @ AisBcIy-rW" sold. The -rW is not being removed.
1

Your pattern and syntax is wrong.

import pandas as pd

# set up the df
df = pd.DataFrame.from_dict(({'Name': '"George @ ACkDk02gfe" sold'},))

# use a raw string for the pattern
df['Name'] = df['Name'].str.replace(r'^"(\w+)\s@.*?"', '\\1')

Comments

0

I'll let someone else post a regex answer, but this could also be done with split. I don't know how consistent the data you are looking at is, but this would work for the provided string:

df['Name'] = df['Name'].str.split(' ').str[0].str[1:] + ' ' + df['Name'].str.split(' ').str[-1]

output:

George sold

Comments

0

This should do for you Split the string by a chain of whitespace,@,text immediately after @and whitespace after the text. This results in a list. remove the list corner brackets while separating elements by space using .str.join(' ')

df.Name=df.Name.str.split('\s\@\s\w+\s').str.join(' ')



 0    George sold

1 Comment

Does this help?
-1

To use a regex for replacement, you need to import re and use re.sub() instead of .replace().

import re
                      Name

"George @ ACkDk02gfe" sold

df['Name'] = re.sub(r"@.*$", "", df['Name'])

should work.

Comments

-1
import re
ss = '"George @ ACkDk02gfe" sold'
ss = re.sub('"', "", ss)
ss = re.sub("\@\s*\w+", "", ss)
ss = re.sub("\s*", " ", ss)

George sold

Given that this is the general format of your code, here's what may help you understand the process I made. (1) substitute literal " (2) substitute given regex \@\s*\w+ (means with literal @ that may be followed by whitespace/s then an alphanumeric word with multiple characters) will be replaced (3) substitute multiple whitespaces with a single whitespace.

You can wrap around a function to this process which you can simply call to a column. Hope it helps!

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.