【问题标题】:Foward filling moving limit正向灌装移动限位
【发布时间】:2021-03-24 21:14:22
【问题描述】:

我有一个包含很多空白的数据框。图片上的第一张桌子。我想到达正确的桌子。我的想法是使用带有移动限制的 ffill() 。限制将根据右侧的内容进行调整。 因此,首先我们计算右侧的连续元素并填充 level2(黄色),然后对 level1(绿色)执行相同的操作。有没有可能?

【问题讨论】:

    标签: python pandas dataframe variables fill


    【解决方案1】:

    假设空单元格是空字符串(""),你可以试试:

    df[df == ""] = np.nan
    m = ~df["Level 1"].isna()
    df.loc[m, "Level 2"] = ""
    df.loc[m, "Level 3"] = ""
    df.loc[:, ["Level 1", "Level 2"]] = df.loc[:, ["Level 1", "Level 2"]].ffill()
    print(df.fillna(""))
    

    打印:

          Level 1   Level 2   Level 3
    0   President                    
    1   President    Office          
    2   President    Office    Lucien
    3   President    Office   Theresa
    4         MEP                    
    5         MEP    Bureau          
    6         MEP    Bureau    Martin
    7         MEP    Bureau  Juliette
    8         MEP    Bureau     Romeo
    9      Groups                    
    10     Groups  Comittee          
    11     Groups  Comittee      Paul
    12     Groups  Comittee      Marc
    13     Groups   Sub Com          
    14     Groups   Sub Com    Julius
    15     Groups   Sub Com    Marcus
    16     Groups   Sub Com  Aurelius
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多