从以前的列填充空白单元格 pandas [英] Fill blank cells from previous columns pandas python

查看:128
本文介绍了从以前的列填充空白单元格 pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个简单的数据框架,例如:

If I have a simple dataframe such as:

q_1     q_1_mark    q_2    q_2_mark
TRUE    a           1      c
FALSE   b           2   
TRUE    c           3   

如何填写空白值q_2_mark列,所以他们与q_1_mark匹配?所以步骤是:

How could I fill in the blank values of the q_2_mark column, so they matched with q_1_mark? So the steps would be:


  1. 查看列并找到空白值。

  2. 查看进入上一个_mark列,并且仅为空白单元格带来价值。

我可以做强力复制和粘贴样式格式,但是这会丢失任何与前一列不同的单元格,即 - > c

I can do brute force copy and paste style formatting, but this loses any cells that contain anything different from the previous column, i.e a --> c

推荐答案

假设您的数据框为 df ,尝试:

Assuming your dataframe is df, Try:

df.q_2_mark = df.q_2_mark.combine_first(df.q_1_mark) 

     q_1 q_1_mark  q_2 q_2_mark
0   True        a    1        c
1  False        b    2        b
2   True        c    3        c

这篇关于从以前的列填充空白单元格 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆