Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove certain string from entire column in pandas dataframe

I have a pandas dataframe df with the contents below:

  Date          Factor       Expiry         Grade  
0 12/31/1991    2.138766     3/30/1992      -3.33% 
1 10/29/1992    2.031381     2/8/1993       -1.06% 
2 5/20/1993     2.075670     6/4/1993       -6.38% 

I would like the remove the % character from all the rows in the Grade column. The result should look like this:

  Date          Factor     Expiry        Grade  
0 12/31/1991    2.138766   3/30/1992     -3.33 
1 10/29/1992    2.031381   2/8/1993      -1.06 
2 5/20/1993     2.075670   6/4/1993      -6.38 

I am using Python v3.6.

like image 723
user3848207 Avatar asked Aug 10 '18 03:08

user3848207


People also ask

How do I remove a value from a column in pandas?

Use drop() method to delete rows based on column value in pandas DataFrame, as part of the data cleansing, you would be required to drop rows from the DataFrame when a column value matches with a static value or on another column value.

How do you remove a specific word from a DataFrame in Python?

To remove a specific word in a string variable in Python, the easiest way is to use the Python built-in string replace() function. string_with_words = "This is a string." string_without_is = string_with_words. replace("is","") print(string_without_is) #Output: This a string.

How do you strip a string in pandas?

The str. strip() function is used to remove leading and trailing characters. Strip whitespaces (including newlines) or a set of specified characters from each string in the Series/Index from left and right sides. Equivalent to str.


Video Answer


2 Answers

Using str.replace would work:

df['Grade'] = df['Grade'].str.replace('%', '')
like image 68
Shaido Avatar answered Oct 18 '22 21:10

Shaido


You can use string slicing and then convert to a numeric type via pd.to_numeric:

df['Grade'] = pd.to_numeric(df['Grade'].astype(str).str[:-1], errors='coerce')

Conversion to float is recommended as a series of strings will be held in a generic and inefficient object dtype, while numeric types permit vectorised operations.

like image 25
jpp Avatar answered Oct 18 '22 20:10

jpp