我正在尝试复制一个类似“正确填充”的 excel 函数,该函数会正确填充值,直到下一个值不为 null/nan/empty。仅当紧接的下一行中的值不为空或不为“nan”时,才执行此“右填充”练习。 我有以下 Pandas 数据框数据集。我当前的输入表是“有”。我的输出表是“想要的”。
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"]) \
})
最佳答案
在第 2 行创建一个 bool 掩码。
- 测试它是否为空(
None
或np.nan
) - 测试它是否等于一个空字符串
''
作业
- 使用
loc
赋值 replace
默认情况下向前填充空值。
cond = have.loc[1].isnull() | have.loc[1].ne('')
have.loc[0, cond] = have.loc[0, cond].replace('', None)
have
如果空格 ''
是空格 ' '
我们可以使用 strip
cond = have.loc[1].isnull() | have.loc[1].ne('')
have.loc[0, cond] = have.loc[0, cond].str.strip().replace('', None)
have
关于python pandas 右填充值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41386073/