根据其他列的值填充熊猫的缺失值

时间:2019-02-21 10:27:16

标签: python python-3.x pandas dataframe

我有以下数据框:

    A    B   C   D
0  NaN  2.0 NaN  0
1  3.0  4.0 NaN  1
2  NaN  NaN NaN  5
3  NaN  3.0 NaN  4

现在我想用B或D中的值填充A的空值,即,如果B中的值是Null,则要检查D。所以结果数据帧看起来像这样。

   A    B   C    D
0  2.0  2.0 NaN  0
1  3.0  4.0 NaN  1
2  5    NaN NaN  5
3  3.0  3.0 NaN  4

我可以使用以下代码执行此操作:

df['A'] = df['A'].fillna(df['B'])
df['A'] = df['A'].fillna(df['D'])

但是我想一行完成,该怎么做?

2 个答案:

答案 0 :(得分:3)

您可以简单地将两个ModifyWordPartButton链接起来:

render()

或将this.props.childrenthis.props.children.map( child => { React.cloneElement(child, this.state) } ) 一起使用:

IconButton

答案 1 :(得分:2)

如果因为许多列更好而不需要链,则使用回填缺失值,并按位置填充第一列:

    public class AResponse
    {
        public string Message { get; set; }
        public bool Flag { get; set; }
    }

    public class BResponse
    {
        public string Message { get; set; }
        public int Count { get; set; }
    }

    [Route("a")]
    public class ControllerA : ApiController
    {
        [HttpGet]
        public AResponse Get()
        {
            return new AResponse
            {
                Message = "Hello from A",
                Flag = true
            };
        }
    }

    [Route("b")]
    public class ControllerB : ApiController
    {
        [HttpGet]
        public BResponse Get()
        {
            return new BResponse
            {
                Message = "Hello from B",
                Count = 42
            };
        }
    }