如何在索引数据帧中将NaN值替换为0

时间:2019-01-23 04:49:54

标签: python pandas

enter image description here

如何将NaN值替换为0,同时保持此索引格式?

1 个答案:

答案 0 :(得分:0)

您可以先将原始索引作为系列处理,然后重新分配索引:

class _CardStackState extends State<CardStackWidget> {
  List<Widget> cards;
  double lastCardElevation = 1.0;
  double lastCardYPosition = 0.0;

  void buildCards(BuildContext context) {
    this.cards = List();
    widget.cardHeaderBodyMap.forEach((header, body) {
      cards.add(_CardStackItem(
        header: header,
        body: body,
        cardElevation: lastCardElevation++,
        cardBackgroundColor: widget.cardBackgroundColor,
        cardCornerRadius: widget.cardCornerRadius,
        cardYPosition: lastCardYPosition,
        screenHeight: MediaQuery.of(context).size.height,
      ));
      lastCardYPosition += widget.cardHeaderHeight;
    });
  }

  @override
  Widget build(BuildContext context) {
    buildCards(context);
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints viewportConstraints) {
        return SingleChildScrollView(
          child: ConstrainedBox(
            constraints: BoxConstraints(
              minHeight: viewportConstraints.maxHeight,
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                (widget.headerWidget != null)
                    ? widget.headerWidget
                    : Container(),
                Container(
                  height: 1000,
                  margin: EdgeInsets.only(top: 4.0),
                  child: Stack(
                    children: cards,
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}

输出:

import pandas as pd
import numpy as np
df = pd.DataFrame({'Column1': [1, 2, 3, 4], 'Column2': [5, 6, 7, 8], 
'Column3': [8, 9, 10, 11]}, index=['a', 'b', np.nan, np.nan])
df.index = pd.Series(df.index).replace(np.nan, 0)
print df