0

for example, I have a column named Children in data frame of python,

few names are [ tom (peter) , lily, fread, gregson (jaeson 123)] etc.

I want to ask that what code I should write, that could remove part of each name staring with bracket e.g '(' and so on. So that from my given names example tom(peter) will become tom in my column and gregson (123) would become gregson. Since there are thousands of names with bracket part and I want to remove the part of string staring from bracket '(' and ending on bracket ')'. This is a data frame of many columns but i want to do this editing in one specific column named as CHILDREN in my dataframe named DF.

2
  • Are you talking about the pandas module ? You say you have a dataframe, i think this type is only in pandas module Commented Apr 12, 2020 at 19:52
  • 1
    try pandas.Series.str.replace with regex Commented Apr 12, 2020 at 19:58

2 Answers 2

2

As suggested by @Ruslan S., you can use pandas.Series.str.replace or you could also use re.sub (and there are other methods as well):

import pandas as pd
df = pd.DataFrame({"name":["tom (peter)" , "lily", "fread", "gregson (jaeson 123)"]})
# OPTION 1 with str.replace :
df["name"] = df["name"].str.replace(r"\([a-zA-Z0-9\s]+\)", "").str.strip()
# OPTION 2 :with re sub
import re
r = re.compile(r"\([a-zA-Z0-9\s]+\)")
df["name"] = df["name"].apply(lambda x: r.sub("", x).strip())

And the result in both cases:

      name
0      tom
1     lily
2    fread
3  gregson

Note that I also use strip to remove leading and trailing whitespaces here. For more info on the regular expression to use, see re doc for instance.

Sign up to request clarification or add additional context in comments.

Comments

0

You can try:

#to remove text between () 
df['columnname'] = df['columnname'].str.replace(r'\((.*)\)', '')
#to remove text between %%
df['columnname'] = df['columnname'].str.replace(r'%(.*)%', '')

2 Comments

Thanks, would you please explain concept, working of this piece of code. so that i could use it for other things also. E.g to change tom %45% to tom i.e in case if '%' is used instead of bracket
r'((.*))' is regular expression meaning everything between' (' and ')' for %45% you have to r'%(.*)%'

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.