如何删除 Pandas DataFrame 中的一行并重新标记索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13807758/
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
How to delete a row in a Pandas DataFrame and relabel the index?
提问by user394430
I am reading a file into a Pandas DataFrame that may have invalid (i.e. NaN) rows. This is sequential data, so I have row_id+1 refer to row_id. When I use frame.dropna(), I get the desired structure, but the index labels stay as they were originally assigned. How can the index labels get reassigned 0 to N-1 where N is the number of rows after dropna()?
我正在将文件读入 Pandas DataFrame 中,该文件可能包含无效(即 NaN)行。这是顺序数据,所以我有 row_id+1 指的是 row_id。当我使用 frame.dropna() 时,我得到了所需的结构,但索引标签保持原样。如何将索引标签重新分配为 0 到 N-1,其中 N 是 dropna() 之后的行数?
回答by Aman
Use pandas.DataFrame.reset_index(), the option drop=Truewill do what you are looking for.
使用pandas.DataFrame.reset_index(),该选项drop=True将满足您的要求。
In [14]: df = pd.DataFrame(np.random.randn(5,4))
In [15]: df.ix[::3] = np.nan
In [16]: df
Out[16]:
0 1 2 3
0 NaN NaN NaN NaN
1 1.895803 0.532464 1.879883 -1.802606
2 0.078928 0.053323 0.672579 -1.188414
3 NaN NaN NaN NaN
4 -0.766554 -0.419646 -0.606505 -0.162188
In [17]: df = df.dropna()
In [18]: df.reset_index(drop=True)
Out[18]:
0 1 2 3
0 1.895803 0.532464 1.879883 -1.802606
1 0.078928 0.053323 0.672579 -1.188414
2 -0.766554 -0.419646 -0.606505 -0.162188

