Reputation: 435
I am trying to replicate a "right fill" excel-like function which fills the values right till the next value is not null/nan/empty. This "right-fill" exercise is only to be done if the value in the immediate following row in not empty or "nan". I have the following pandas dataframe dataset. My current input table is "have". My output table is "want".
import pandas as pd
have = pd.DataFrame({ \
"0": pd.Series(["abc","1","something here"]) \
,"1": pd.Series(["","2","something here"]) \
,"2": pd.Series(["","3","something here"]) \
,"3": pd.Series(["something","1","something here"]) \
,"4": pd.Series(["","2","something here"]) \
,"5": pd.Series(["","","something here"]) \
,"6": pd.Series(["","","something here"]) \
,"7": pd.Series(["cdf","5","something here"]) \
,"8": pd.Series(["","6","something here"]) \
,"9": pd.Series(["xyz","1","something here"]) \
})
want = pd.DataFrame({ \
"0": pd.Series(["abc","1","something here"]) \
,"1": pd.Series(["abc","2","something here"]) \
,"2": pd.Series(["abc","3","something here"]) \
,"3": pd.Series(["something","1","something here"]) \
,"4": pd.Series(["something","2","something here"]) \
,"5": pd.Series(["","","something here"]) \
,"6": pd.Series(["","","something here"]) \
,"7": pd.Series(["cdf","5","something here"]) \
,"8": pd.Series(["cdf","6","something here"]) \
,"9": pd.Series(["xyz","1","something here"]) \
})
Upvotes: 3
Views: 2208
Reputation: 294228
Create a boolean mask on row 2.
None
or np.nan
)''
Assignment
loc
to assignreplace
forward fills nulls by default.cond = have.loc[1].isnull() | have.loc[1].ne('')
have.loc[0, cond] = have.loc[0, cond].replace('', None)
have
If the blanks ''
are white space ' '
we can use strip
cond = have.loc[1].isnull() | have.loc[1].ne('')
have.loc[0, cond] = have.loc[0, cond].str.strip().replace('', None)
have
Upvotes: 2