hkml
hkml

Reputation: 339

Remove parenthesis and contents in parenthesis if present in a df column

I have a dataframe where the top scores/instances have parenthesis. I would like to remove the parenthesis and only leave the number. How would I do so?

I have tried the code below, but it leaves me with nans for all other numbers that do not have paranthesis.

.str.replace(r"\(.*\)","")

This is what the columns look like:

0    1(1P)
1    3(3P)
2    2(2P)
3    4(RU)
4    5(RU)
5    6(RU)
6        8
7        7
8       11
9       13

I want clean columns with only numbers.

Thanks!

Upvotes: 1

Views: 52

Answers (1)

jezrael
jezrael

Reputation: 863166

Reason is mixed values - numeric with strings, possible solution is:

df['a'] = df['a'].astype(str).str.replace(r"\(.*\)","").astype(int)
print (df)
    a
0   1
1   3
2   2
3   4
4   5
5   6
6   8
7   7
8  11
9  13

Upvotes: 1

Related Questions