[Python] How to replace None only with empty string using pandas?

Discussão em 'Python' iniciado por Stack, Setembro 14, 2025.

  1. Stack

    Stack Membro Participativo

    the code below generates a df:

    import pandas as pd
    from datetime import datetime as dt
    import numpy as np

    dates = [dt(2014, 1, 2, 2), dt(2014, 1, 2, 3), dt(2014, 1, 2, 4), None]
    strings1 = ['A', 'B',None, 'C']
    strings2 = [None, 'B','C', 'C']
    strings3 = ['A', 'B','C', None]
    vals = [1.,2.,np.nan, 4.]
    df = pd.DataFrame(dict(zip(['A','B','C','D','E'],
    [strings1, dates, strings2, strings3, vals])))



    +---+------+---------------------+------+------+-----+
    | | A | B | C | D | E |
    +---+------+---------------------+------+------+-----+
    | 0 | A | 2014-01-02 02:00:00 | None | A | 1 |
    | 1 | B | 2014-01-02 03:00:00 | B | B | 2 |
    | 2 | None | 2014-01-02 04:00:00 | C | C | NaN |
    | 3 | C | NaT | C | None | 4 |
    +---+------+---------------------+------+------+-----+


    I would like to replace all None (real None in python, not str) inside with ''(empty string).

    The expected df is

    +---+---+---------------------+---+---+-----+
    | | A | B | C | D | E |
    +---+---+---------------------+---+---+-----+
    | 0 | A | 2014-01-02 02:00:00 | | A | 1 |
    | 1 | B | 2014-01-02 03:00:00 | B | B | 2 |
    | 2 | | 2014-01-02 04:00:00 | C | C | NaN |
    | 3 | C | NaT | C | | 4 |
    +---+---+---------------------+---+---+-----+


    what I did is

    df = df.replace([None], [''], regex=True)

    But I got

    +---+---+---------------------+---+------+---+
    | | A | B | C | D | E |
    +---+---+---------------------+---+------+---+
    | 0 | A | 1388628000000000000 | | A | 1 |
    | 1 | B | 1388631600000000000 | B | B | 2 |
    | 2 | | 1388635200000000000 | C | C | |
    | 3 | C | | C | | 4 |
    +---+---+---------------------+---+------+---+

    1. all the dates becomes big numbers
    2. Even NaT and NaN are replaced, which I don't want.

    How can I achieve that correctly and efficently?

    Continue reading...

Compartilhe esta Página