python pandas 右填充值

标签 python pandas

我正在尝试复制一个类似“正确填充”的 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 掩码。

  • 测试它是否为空(Nonenp.nan)
  • 测试它是否等于一个空字符串''

作业

  • 使用loc赋值
  • replace 默认情况下向前填充空值。

cond = have.loc[1].isnull() | have.loc[1].ne('')
have.loc[0, cond] = have.loc[0, cond].replace('', None)
have

enter image description here


如果空格 '' 是空格 ' ' 我们可以使用 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/

相关文章:

python - pandas 分组然后选择某些列

php - 需要使用 python 输出 json 的帮助

python - 在python中增加财务季度

python - 如何将数据帧输出插入到mysql

python - 将 Pandas 时间序列切成 n 个月的 block

python - Pandas Dataframe 过滤器不工作,但 str.match() 正在工作

python - 限制 python 3 中的无效输入

Python网络爬虫找不到存在的关键字

python - 使用 Python 进行 Excel 和列表

python - 两个图像相减