Search code examples
pythonpandasdata-cleaningpreprocessor

remove bracket and its content in python


I have implemented an emotion analysis classification using lstm method. I have already train my model and saved it. I have load the train model and I am doing the classification part where I am saving it in a dataframe. I need to remove brackets along with its content I will show you below.

here are my codes:

 hotelname = []
sentimentanalysis = []

for item in selection1:
    name = item['name']
    hotelname.append(name)
    print (name)

the output is as follows:

Mystik Lifestyle (Save 34%)
Chalets Chamarel (Adults Only)
Andrea Lodge (Save 18%)
Hibiscus Beach Resort & Spa (Save 18%)
Lagoon Attitude (Adults Only)
Ocean V Hotel (Adults Only)

but I want my output to be like this::

Mystik Lifestyle 
Chalets Chamarel 
Andrea Lodge 
Hibiscus Beach Resort & Spa 
Lagoon Attitude 
Ocean V Hotel 

can someone please tell me what do I need to add in my codes please guys.


Solution

  • You can do it by using the simple String method called .split().
    Look at the code below:

    elements = ['Mystik Lifestyle (Save 34%)',
    'Chalets Chamarel (Adults Only)',
    'Andrea Lodge (Save 18%)',
    'Hibiscus Beach Resort & Spa (Save 18%)',
    'Lagoon Attitude (Adults Only)',
    'Ocean V Hotel (Adults Only)']
    
    for element in elements:
        without_text_after_unwanted_character = element.split('(')[0] 
        # This will get everything before the '(' as we splitted
        print(without_text_after_unwanted_character)
    
    # If you want to create a new list the new values, you can do:
    clean_list = [x.split('(')[0] for x in elements]
    
    for clean_text in clean_list:
        print(clean_text)