Home > Mobile >  Remove unwanted part of strings in a column with Pandas
Remove unwanted part of strings in a column with Pandas

Time:12-24

I have a column that looks like this

        Message

0       Wings.cpp:222] Current sidewing pressure: 3410
1       Wings.cpp:222] Current sidewing pressure: 4206
2       Wings.cpp:222] Current sidewing pressure: 3433
3       Wings.cpp:222] Current sidewing pressure: 4229
4       Position.cpp:438] <AGVPOS> 602, 7787.496, -920...

I would like to get rid of the part that starts from the beginning of the string until "] ".

I have tried to do it myself using this.

df['Message'] = df['Message'].astype("str")
df['Message'] = df['Message'].apply(lambda x: x[ x.index(']') :])

But I get this error : ValueError: substring not found

Is there a way to fix this one or just some other more efficient way?

Thank you.

CodePudding user response:

Use Series.replace:

df['Message'] = df['Message'].replace('^.*\]\s*','', regex=True)

CodePudding user response:

Use str.replace:

df["Message"] = df["Message"].str.replace('^.*?\]\s*', '')

Here is a regex demo showing the logic is working.

  • Related