pandas 数据填充

可以使用前面 后面 上面 下面值填充

 df = pd.DataFrame({
        "animal": ["", "安全能力", "1", ],
        "color": ["", "安全能力", "5.1.1"],
        "name": ['安全能力等级', "一级", "√"],
        "name1": ["", "二级", "√"],
        "name2": ["", "三级级", "√"],
        "name3": ["", "四级", "√"],
        "age": ["", "五级", "√"],
    })



    df2 = df.replace('', None)
    df2 = df2.fillna(method='pad', axis=0)
    df2 = df2.fillna(method='pad', axis=1)
    df2 = df2.fillna(method='backfill', axis=1)
    df2 = df2.fillna(method='backfill', axis=0)

    print(df2)
posted @ 2023-10-18 17:56  vx_guanchaoguo0  阅读(35)  评论(0编辑  收藏  举报