Python基于组设置右填充值

Python基于组设置右填充值,python,pandas,Python,Pandas,我正在尝试复制一个类似“right fill”的excel函数,该函数将正确填充值,直到下一个值不为null/nan/empty。只有在紧接着的下一行中的值不为空或“nan”时,才能执行此“右填充”操作。此外,每个小组都必须这样做。我有以下数据帧数据集。我当前的输入表是“have”。我的输出表是“want” 我只是python的初学者。因此,任何帮助都将不胜感激。 此外,对于希望在分组操作中执行此操作的人员,数据如下: 表“have”与分组字段“groups”如下所示: 带有分组字段“组”的表“

我正在尝试复制一个类似“right fill”的excel函数,该函数将正确填充值,直到下一个值不为null/nan/empty。只有在紧接着的下一行中的值不为空或“nan”时,才能执行此“右填充”操作。此外,每个小组都必须这样做。我有以下数据帧数据集。我当前的输入表是“have”。我的输出表是“want”

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

带有分组字段“组”的表“需要”:

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

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

谢谢你,皮尔斯。U天才:)
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"]) \
    })
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
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)