Deleting certain strings in a DataFrame column (Python)

Viewed 35

I have a DataFrame like this one:

  Column 1  Column 2
0    A B C        34
1    D E F        42
2    G H I        42

I want to delete certain strings from (not all strings) from the Column 1. The result I expect:

  Column 1  Column 2
0        C        34
1        F        42
2        I        42

Any solutions will be accepted.


1 Answers

You can use str.extract to get the last non space part with a regex:

df['Column 1'] = df['Column 1'].str.extract(r'(\S+$)')

Or str.split on spaces and get the last item with the str accessor:

df['Column 1'] = df['Column 1'].str.split(r'\s+').str[-1]

output:

  Column 1  Column 2
0        C        34
1        F        42
2        I        42
deletion approach

Because you asked for "deleting" a part of the string, the approach would be to use str.replace:

df['Column 1'] = df['Column 1'].str.replace(r'.*\s+', '', regex=True)

although probably not the best suited one here.

Related