基于组的右填充值

2024-04-26 06:52:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试复制一个类似于“right fill”的excel函数,它将正确填充值,直到下一个值不是null/nan/empty。只有在紧跟其后一行中的值不为空或“nan”时,才可以进行“右填充”练习。此外,每个小组都必须这样做。我有以下数据帧数据集。我当前的输入表是have。我的输出表是“want”。你知道吗

我只是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

Tags: 数据herehavelocsomethingseriespdgroups
1条回答
网友
1楼 · 发布于 2024-04-26 06:52:27
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)

enter image description here

相关问题 更多 >