在熊猫数据框中,\\ N是随机存在的,我想将其删除

时间:2020-03-13 08:15:58

标签: python pandas dataframe

我做了一个pandas.dataframe

我用NAN杀死了pandas.dropna,但是\\N并没有删除dropna

请告诉我如何摆脱它...

1 个答案:

答案 0 :(得分:2)

请告诉我这是否有帮助

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

代码可能类似于:

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)

结果:

       0  1
0  test1  1
2  test2  3
5  test3  6