gpt4 book ai didi

Python Pandas 根据组右填充值

转载 作者:太空宇宙 更新时间:2023-11-03 15:49:38 24 4
gpt4 key购买 nike

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

我只是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"]) \
})

带有分组字段“group”的表“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

最佳答案

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

关于Python Pandas 根据组右填充值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41424692/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com