2017-05-01 142 views
1

給出一個DF刪除行的索引數據幀大熊貓

in[0]df1 
out[0] 
     DATE REVENUE COST POSITION 
FACTOR 
0 2017/01/01 1000 900 10 
1 2017/01/01 900  700 9 
2 2017/01/01 1100 800 7 

我有一個附加行FACTOR。嘗試reset_index()和其他方式後,我無法刪除FACTOR多行(行)索引。有沒有辦法做到這一點?

我知道刪除列和重置索引很常見,但不是這樣。

回答

4

我希望這個作品:)

df.reset_index(inplace=True) # Resets the index, makes factor a column 
df.drop("Factor",axis=1,inplace=True) # drop factor from axis 1 and make changes permanent by inplace=True 
3

嘗試使用:

df1.reset_index(drop=True) 

這將重置指數爲默認的整數索引,並刪除原來的一個。 如果你要分配給此改變原來dataframe它更容易使用:

df1.reset_index(drop=True, inplace=True) 

正如將編輯df1數據框中未做它的一個副本。

2

FACTOR是索引的名字 - 你不應該擔心 - 它不會影響你的數據:如果您想將其重命名或擺脫它(保留

In [78]: df 
Out[78]: 
       DATE REVENUE COST POSITION 
FACTOR 
10  2017/01/01  1000 900  10 
11  2017/01/01  900 700   9 
12  2017/01/01  1100 800   7 

In [79]: df.index.name 
Out[79]: 'FACTOR' 

指數值),可以使用DataFrame.rename_axis()方法:

In [80]: df = df.rename_axis(None) 

In [81]: df 
Out[81]: 
      DATE REVENUE COST POSITION 
10 2017/01/01  1000 900  10 
11 2017/01/01  900 700   9 
12 2017/01/01  1100 800   7 

In [82]: df.index.name is None 
Out[82]: True