manoj kumar
manoj kumar

Reputation: 115

remove extra characters or a value from column

How to remove the '/' and after the '/' value i.e, 25?

sample:

   score
    10
    24
    19
    20/25
    18/25
    16/25

output:

score
10
24
19
20
18
16

Upvotes: 3

Views: 107

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133428

You could use df.replace function here.

df['score'] = df['score'].replace('/.*','',regex=True)

df will become like:

    score
0   10
1   24
2   19
3   20
4   18
5   16

Upvotes: 6

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520878

Using str.extract:

df['score'] = df['score'].str.extract(r'^(\d+)')

Using str.replace:

df['score'] = df['score'].str.replace(r'/.*$', '')

Upvotes: 2

Related Questions