Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing backslashes from values in a Pandas dataframe

I have a Pandas dataframe that contains backslashes. I want to strip out those backslashes, but I can't get the replace function to work. Here's what I'm doing:

df=pd.DataFrame(data={'col1':['a\\b','ab'], 'col2':['c','cd\\']})
df.replace(to_replace='\\', value='', regex=True, inplace=True)

When I run this, I get an error message that says:

error: bad escape (end of pattern) at position 0

If I remove "regex=True", I don't get the error, but nothing get's replaced.

How do I remove the backslashes?

like image 302
ASturt Avatar asked Oct 17 '18 23:10

ASturt


People also ask

How do I change a backslash in Python?

We can use the replace() function to replace the backslashes in a string with another character. To replace all backslashes in a string, we can use the replace() function as shown in the following Python code.

How do you remove infinite values from a DataFrame?

By using replace() & dropna() methods you can remove infinite values from rows & columns in pandas DataFrame. Infinite values are represented in NumPy as np. inf & -np.

How do you drop the last 5 rows in pandas?

Drop Last Row of Pandas DataFrame Using head() Function You can also use df. head(df. shape[0] -1) to remove the last row of pandas DataFrame.

How to strip strip space in column of pandas Dataframe?

Strip Space in column of pandas dataframe (strip leading, trailing & all spaces of column in pandas) 1 Trim leading space of column in pandas – lstrip () 2 Trim trailing space of column in pandas – rstrip () 3 Trim Both leading and trailing space of column in pandas – strip () 4 strip all the white space of column in pandas More ...

How to remove duplicates from pandas Dataframe?

Steps to Remove Duplicates from Pandas DataFrame. 1 Step 1: Gather the data that contains the duplicates. Firstly, you’ll need to gather the data that contains the duplicates. For example, let’s say ... 2 Step 2: Create Pandas DataFrame. 3 Step 3: Remove duplicates from Pandas DataFrame.

How do I remove missing values from a Dataframe in Python?

This method is a simple, but messy way to handle missing values since in addition to removing these values, it can potentially remove data that aren’t null. You can call dropna () on your entire dataframe or on specific columns: # Drop rows with null values df = df.dropna (axis=0) # Drop column_1 rows with null values

How to remove all the extra space from cell’s in pandas?

We will use different methods which will help us to remove all the extra space from the cell’s. Different methods are : Pandas provide predefine method “pandas.Series.str.strip ()” to remove the whitespace from the string. Using strip function we can easily remove extra whitespace from leading and trailing whitespace from staring.


1 Answers

You can use replace

df = df.replace(to_replace= r'\\', value= '', regex=True)
like image 188
Abhi Avatar answered Oct 06 '22 00:10

Abhi