Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to remove random symbols in a dataframe in Pandas?

  • Last Updated : 30 Jun, 2021

In this article, we will see how to remove random symbols in a dataframe in Pandas.

Method 1: Selecting columns

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Syntax: dataframe[columns].replace({symbol:},regex=True)



First, select the columns which have a symbol that needs to be removed. And inside the method replace() insert the symbol example replace(“h”:””)

Python3




import pandas as pd
 
 
df = pd.DataFrame({'A': [1, 2, 3],
                   'B': [4, 5, 6],
                   'C': ['f;', 'd:', 'sda;sd'],
                   'D': ['s', 'd;', 'd;p'],
                   'E': [5, 3, 6],
                   'F': [7, 4, 3]})
 
print(df)
 
cols_to_check = ['C', 'D', 'E']
print(df[cols_to_check])
 
df[cols_to_check] = df[cols_to_check].replace({';': ''}, regex=True)
print(df)

Output:

Method 2: Using dataframe.iloc

Syntax:

dataframe.iloc[].replace({character},regex=True)

In this method, you use dataframe.iloc[] to change the symbols.



Python3




import pandas as pd
 
df = pd.DataFrame({'A': [1, 2, 3],
                   'B': [4, 5, 6],
                   'C': ['f;', 'd:', 'sda;sd'],
                   'D': ['s', 'd;', 'd;p'],
                   'E': [5, 3, 6],
                   'F': [7, 4, 3]})
 
print(df)
 
cols_to_check = ['C', 'D', 'E']
print(df.iloc[[0, 2]])
 
df.iloc[[0, 2]] = df.iloc[[0, 2]].replace({';': ''}, regex=True)
print(df)

Output:

Method 3: Using dataframe.loc[]

Syntax:

dataframe.loc[].replace({character},regex=True)

Python3




import pandas as pd
 
 
df = pd.DataFrame({'A': [1, 2, 3],
                   'B': [4, 5, 6],
                   'C': ['f;', 'd:', 'sda;sd'],
                   'D': ['s', 'd;', 'd;p'],
                   'E': [5, 3, 6],
                   'F': [7, 4, 3]})
 
print(df)
 
cols_to_check = ['C', 'D', 'E']
print(df.loc[:, cols_to_check])
 
 
df.loc[:, cols_to_check] = df.loc[
  :, cols_to_check].replace({';': ''}, regex=True)
print(df)

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :