如何重新索引pandas数据帧以将起始索引值重置为零?

时间:2015-12-09 03:26:50

标签: python pandas

在我的数据框中,某些行中有NaN值。我想删除这些行。我用dataframe.dropna(how =' any')解决了这个问题。结果如下:

         date  time   open   hign    low  close  volume  turnover
2  2015-09-01   931  48.60  48.60  48.00  48.00  449700  21741726
3  2015-09-01   932  47.91  48.33  47.91  48.25  158500   7614508

我想重新索引我的数据帧的行,所以我运行:

length = dataframe.dropna(how='any').shape[0]
dataframe1 = dataframe.index(range(length))

但是dataframe1仍然保留旧的索引值,例如:

          date  time   open   hign    low  close  volume  turnover
0         NaN   NaN    NaN    NaN    NaN    NaN     NaN       NaN
1         NaN   NaN    NaN    NaN    NaN    NaN     NaN       NaN
2  2015-09-01   931  48.60  48.60  48.00  48.00  449700  21741726
3  2015-09-01   932  47.91  48.33  47.91  48.25  158500   7614508

如何使数字以0开头并删除前两行?

期望的结果:

          date  time   open   hign    low  close  volume  turnover
0  2015-09-01   931  48.60  48.60  48.00  48.00  449700  21741726
1  2015-09-01   932  47.91  48.33  47.91  48.25  158500   7614508

2 个答案:

答案 0 :(得分:5)

重置索引并指定drop=True

df = pd.DataFrame({'close': [nan, nan, 48.0, 48.25],
                   'date': [nan, nan, '2015-09-01', '2015-09-01'],
                   'hign': [nan, nan, 48.60, 48.33],
                   'low': [nan, nan, 48.0, 47.91],
                   'open': [nan, nan, 48.60, 47.91],
                   'time': [nan, nan, 931.0, 932.0],
                   'turnover': [nan, nan, 21741726.0, 7614508.0],
                   'volume': [nan, nan, 449700.0, 158500.0]})

>>> df
         date  time   open   hign    low  close  volume  turnover
0         NaN   NaN    NaN    NaN    NaN    NaN     NaN       NaN
1         NaN   NaN    NaN    NaN    NaN    NaN     NaN       NaN
2  2015-09-01   931  48.60  48.60  48.00  48.00  449700  21741726
3  2015-09-01   932  47.91  48.33  47.91  48.25  158500   7614508

>>> df.dropna(how='any').reset_index(drop=True)
         date  time   open   hign    low  close  volume  turnover
0  2015-09-01   931  48.60  48.60  48.00  48.00  449700  21741726
1  2015-09-01   932  47.91  48.33  47.91  48.25  158500   7614508

答案 1 :(得分:-1)

您是否尝试过reindex功能?