0

I'm trying to remove some strings in a dataframe that start with System:

My dataframe:

       A          B                                                                C
  French      house   Blablabla System:Microsoft Windows XP; Browser:Chrome 32.0.1700;
 English      house               my address: 101-102 bd Charles de Gaulle 75001 Paris
  French  apartment                                                    my name is Liam
  French      house                                                       Hello George!
 English  apartment              System:Microsoft Windows XP; Browser:Chrome 32.0.1700;

I tried:

def remove_lines():

    df['C'] = df['C'].str.replace(r'(\s+)(System:).+','')

    return df

Nothing happens...

Good output:

       A          B                                                                C
  French      house                                                          Blablabla 
 English      house               my address: 101-102 bd Charles de Gaulle 75001 Paris
  French  apartment                                                    my name is Liam
  French      house                                                       Hello George!
 English  apartment              

2 Answers 2

3

Use:

df.C = df.C.str.replace('System:.*','')
df.C
# 0                                           Blablabla 
# 1    my address: 101-102 bd Charles de Gaulle 75001...
# 2                                      my name is Liam
# 3                                        Hello George!
# 4                                                     
# Name: C, dtype: object
Sign up to request clarification or add additional context in comments.

Comments

1

You can simply use split function on System and pick the first part, like this:

In [1936]: df.C = pd.DataFrame(df.C.str.split('System').tolist())[0]
In [1937]: df
Out[1937]: 
         A          B                                                  C
0   French      house                                         Blablabla
1  English      house  my address: 101-102 bd Charles de Gaulle 75001...
2   French  apartment                                    my name is Liam
3   French      house                                      Hello George!
4  English  apartment                                                   

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.