在pandas数据框中选择行后重新排序行索引

时间:2015-08-31 06:37:35

标签: python pandas

我有以下数据框:

import pandas as pd
df = pd.DataFrame({ 'gene':["foo","bar","qux","woz"], 'cell1':[5,0,1,0], 'cell2':[12,90,13,0]})
df = df[["gene","cell1","cell2"]]

看起来像这样:

  gene  cell1  cell2
0  foo      5     12
1  bar      0     90
2  qux      1     13
3  woz      0      0

执行行选择后,我得到了这个:

In [168]: ndf = df[(df[['cell1','cell2']] == 0 ).any(axis=1)]

In [169]: ndf
Out[169]:
  gene  cell1  cell2
1  bar      0     90
3  woz      0      0

请注意,现在ndf包含行索引13如何将其编入索引01

被检出的输出是:

   gene  cell1  cell2
0  bar      0     90
1  woz      0      0

我尝试了但失败了:

ndf.reset_index

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

尝试这个

ndf.reset_index(drop = True)
相关问题