Borut Flis
Borut Flis

Reputation: 16375

Changing all values in one column of Pandas data frame

I have a pandas Data Frame with 22 columns, I want to modify all the values in the column 'Result'.

In my file the values from the result column are strings in the form 'W (+3)', I want to extract the integer, basically the thing inside the paranthesis.

a[a.find("(")+1:a.find(")")]

I just use the find function and index the string. But how do I apply this to all the values in column 'Result'. I suposse I could iterate through with a for loop, is there any other way?

Upvotes: 1

Views: 470

Answers (2)

Ben Dumoulin
Ben Dumoulin

Reputation: 11

There is an apply function: a['Results'].apply(lambda x: ... ), it's faster than looping through your dataframe

Upvotes: 0

Rakesh
Rakesh

Reputation: 82765

You can use str.extract with a regex pattern

Ex:

import pandas as pd
df = pd.DataFrame({"Result": [ 'W (+3)' ]})
print(df["Result"].str.extract(r"\((.*?)\)"))

Output:

0    +3
Name: Result, dtype: object

Upvotes: 2

Related Questions