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.
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.
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.
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.
Using str.replace
would work:
df['Grade'] = df['Grade'].str.replace('%', '')
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With