user1896796
user1896796

Reputation: 749

Replacing values in a column in pandas

I have a df row like below lets call the columns as col1, col2, col3 ad col4-

 facility   20% xyzzz   facility   40% alsnan

I want to remove to compare the values in 2nd and 4th column. For that I want only the 20% and 40% in the column.

I am thinking if my output looks like -

 facility   20%  facility   40%

it would be easier to compare the values.

How can I achieve this.

Upvotes: 0

Views: 49

Answers (2)

pedro_bb7
pedro_bb7

Reputation: 2091

df.drop('column name', axis=1, inplace=True) #if you want to replace permantly the data frame

Upvotes: 0

Chris
Chris

Reputation: 29742

Use pandas.Series.str.extract:

for c in ['col2', 'col4']:
    df[c] = df[c].str.extract('(\d+%)')
print(df)

Output:

       col1 col2      col3 col4
0  facility  20%  facility  40%

Upvotes: 2

Related Questions