In the pandas dataframe, \\ N is randomly exist and i want to remove it

443 Views Asked by At

I made a pandas.dataframe.

I got rid of NAN with pandas.dropna, but \\N wasn't removed by dropna.

Please tell me how I can get rid of it.

1

There are 1 best solutions below

0
JvdV On BEST ANSWER
df = df.replace(r'^\\N$', np.nan, regex=True).dropna()

Code could be like:

import pandas as pd
import numpy as np
from numpy import nan

df = pd.DataFrame([
    ['test1', 1],
    ['\\N', 2],
    ['test2', 3],
    [nan, 4],
    ['\\N', 5],
    ['test3', 6]])

df = df.replace(r'^\\N$', np.nan, regex=True).dropna()
print(df)

Result:

       0  1
0  test1  1
2  test2  3
5  test3  6