Python 重新索引数据帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15557542/
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
Reindexing dataframes
提问by user2133151
I have a data frame. Then I have a logical condition using which I create another data frame by removing some rows. The new data frame however skips indices for removed rows. How can I get it to reindex sequentially without skipping? Here's a sample coded to clarify
我有一个数据框。然后我有一个逻辑条件,使用它我通过删除一些行来创建另一个数据框。但是,新数据框会跳过已删除行的索引。如何让它在不跳过的情况下按顺序重新索引?这是一个示例编码以澄清
import pandas as pd
import numpy as np
jjarray = np.array(range(5))
eq2 = jjarray == 2
neq2 = np.logical_not(eq2)
jjdf = pd.DataFrame(jjarray)
jjdfno2 = jjdf[neq2]
jjdfno2
Out:
出去:
0
0 0
1 1
3 3
4 4
I want it to look like this:
我希望它看起来像这样:
0
0 0
1 1
2 3
3 4
Thanks.
谢谢。
采纳答案by DSM
One way is to use reset_index:
一种方法是使用reset_index:
>>> df = pd.DataFrame(range(5))
>>> eq2 = df[0] == 2
>>> df_no_2 = df[~eq2]
>>> df_no_2
0
0 0
1 1
3 3
4 4
>>> df_no_2.reset_index(drop=True)
0
0 0
1 1
2 3
3 4

