2017-01-02 138 views
1

我想複製一個「正確的填充」類似excel的函數,它填充值的權利,直到下一個值不爲null /南/空。這個「正確填充」練習只有在緊接着的下一行中的值不爲空或「南」時才能完成。而且,這必須爲每個小組完成。我有以下熊貓數據框數據集。我目前的輸入表是「有」。我的輸出表是「想要」。Python熊貓根據組填充值

我只是一個Python初學者。所以任何幫助,將不勝感激。 誰也爲那些想這種操作上的集團化運作來進行,數據如下: 表「有」與分組字段「組」如下:

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

表「希望」與分組字段「組」:

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 

回答

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) 

enter image description here

+0

謝謝piRSquared。 U天才:) – Seb