user11035754
user11035754

Reputation: 227

How to handle empty cells that pandas does not recognize?

I am data preprocessing using nlp techniques. When I try to remove the URL links from the dataframe, those rows that had URL links become blank entries or non-ascii characters. These blanks are seen in the excel file when I download the dataframe as csv. However, when I implement the following code

df.replace('', np.nan, inplace=True) OR
df['columnName'].replace('', np.nan, inplace=True)

this code outputs there are no NULL or empty values in the dataframe but in reality there are. How to deal with this situation? I want to delete all such empty rows

Upvotes: 0

Views: 1759

Answers (1)

BENY
BENY

Reputation: 323306

You can just using str.strip before replace

df['columnName']=df['columnName'].str.strip().replace('',np.nan)

Working around example

s=pd.Series(['   ','llllll'])
s.str.strip().replace('',np.nan)
0       NaN
1    llllll
dtype: object

Upvotes: 1

Related Questions