【问题标题】:Python Pandas right fill values based on groupsPython Pandas 根据组正确填充值
【发布时间】:2017-01-03 03:54:03
【问题描述】:

我正在尝试复制一个“正确填充”类似 excel 的函数,该函数填充值直到下一个值不是 null/nan/empty。仅当紧随其后的行中的值不为空或“nan”时才进行此“右填充”练习。此外,这必须针对每个组进行。我有以下熊猫数据框数据集。我当前的输入表是“有”。我的输出表是“想要的”。

我只是python的初学者。所以任何帮助将不胜感激。 对于那些希望在分组操作中进行此操作的人,数据如下: 表“have”如下,分组字段“groups”:

import pandas as pd
    have = pd.DataFrame({ \
    "groups": pd.Series(["group1","group1","group1","group2","group2","group2"]) \
    ,"0": pd.Series(["abc","1","something here","abc2","1","something here"]) \
    ,"1": pd.Series(["","2","something here","","","something here"]) \
    ,"2": pd.Series(["","3","something here","","3","something here"]) \
    ,"3": pd.Series(["something","1","something here","something","1","something here"]) \
    ,"4": pd.Series(["","2","something here","","2","something here"]) \
    ,"5": pd.Series(["","","something here","","","something here"]) \
    ,"6": pd.Series(["","","something here","","","something here"]) \
    ,"7": pd.Series(["cdf","5","something here","mnop","5","something here"]) \
    ,"8": pd.Series(["","6","something here","","6","something here"]) \
    ,"9": pd.Series(["xyz","1","something here","xyz","1","something here"]) \
    })

带有分组字段“groups”的表“want”:

import pandas as pd
    want = pd.DataFrame({ \
    "groups": pd.Series(["group1","group1","group1","group2","group2","group2"]) \
    ,"0": pd.Series(["abc","1","something here","anything","1","something here"]) \
    ,"1": pd.Series(["abc","2","something here"," anything ","2","something here"]) \
    ,"2": pd.Series(["abc","3","something here"," anything ","3","something here"]) \
    ,"3": pd.Series(["something","1","something here","","","something here"]) \
    ,"4": pd.Series(["something ","2","something here","","","something here"]) \
    ,"5": pd.Series(["","","something here","","","something here"]) \
    ,"6": pd.Series(["","","something here","","","something here"]) \
    ,"7": pd.Series(["cdf","5","something here","mnop","5","something here"]) \
    ,"8": pd.Series(["cdf ","6","something here"," mnop ","6","something here"]) \
    ,"9": pd.Series(["xyz","1","something here","xyz","1","something here"]) \
    })

我尝试使用此代码,但我仍在尝试熟悉 groupbyapply 语句:

grouped=have.groupby('groups') 
have.groupby('groups').apply(lambda g: have.loc[g].isnull() )
#cond = have.loc[1].isnull() | have.loc[1].ne('')
want.loc[0, cond] = want.loc[0, cond].str.strip().replace('', None)
want

【问题讨论】:

    标签: python pandas


    【解决方案1】:
    def fill(df):
        df = df.copy()
        i0, i1 = df.index[0], df.index[1]
        cond = have.loc[i1].isnull() | have.loc[i1].ne('')
        df.loc[i0, cond] = df.loc[i0, cond].str.strip().replace('', None)
        return df
    
    
    have.groupby('groups', group_keys=False).apply(fill)
    

    【讨论】:

    • 谢谢 piRSquared。你天才:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 2017-05-14
    • 2017-12-05
    • 2021-06-04
    • 1970-01-01
    • 2015-02-14
    • 2020-04-12
    相关资源
    最近更新 更多