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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34170250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:21:35  来源:igfitidea点击:

How can I reindex pandas dataframe to reset the starting index value to zero?

pythonpandas

提问by Peng He

In my dataframe, there are NaN values in some rows. I want to delete these rows. I solve it with dataframe.dropna(how='any'). The result looks like:

在我的数据框中,某些行中有 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

I want to reindex the rows of my dataframe, so I run:

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

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

But dataframe1 still keeps the old index values, like:

但是 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

How can I make the number begin with 0 and delete the first two rows?

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

Desired result:

想要的结果:

          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

回答by Alexander

Reset the index and specify drop=True.

重置索引并指定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

回答by hd1

Did you try the reindexfunctionality?

您是否尝试过重新索引功能?