2017-07-06 84 views
1

我從API獲取DataFrame,但索引不是從0開始的。 我想從0索引它,所以我嘗試了.reindex()。 但是它只是NA以前的項目,有用的線仍然是從哪裏來的... 我如何索引pandas DataFrame中0的有用項目?如何刪除DataFrame無用的行,使有用的行索引從0開始?

In [29]: a = ts.get_k_data('399300', index=True,start='2015-05-01', end='2015-05 
    ...: -31') 

In [30]: a 
Out[30]: 
      date  open close  high  low  volume  code 
78 2015-05-04 4757.64 4787.74 4795.92 4699.40 377843853.0 sz399300 
79 2015-05-05 4785.19 4596.84 4785.19 4572.98 460419626.0 sz399300 
80 2015-05-06 4626.23 4553.33 4700.91 4511.76 376073702.0 sz399300 
81 2015-05-07 4520.82 4470.09 4546.34 4467.46 297759203.0 sz399300 

In [31]: b = a.reindex(list(range(0,80))) 

In [32]: b 
Out[32]: 
      date  open close  high  low  volume  code 
0   NaN  NaN  NaN  NaN  NaN   NaN  NaN 
1   NaN  NaN  NaN  NaN  NaN   NaN  NaN 
2   NaN  NaN  NaN  NaN  NaN   NaN  NaN 
..   ...  ...  ...  ...  ...   ...  ... 
76   NaN  NaN  NaN  NaN  NaN   NaN  NaN 
77   NaN  NaN  NaN  NaN  NaN   NaN  NaN 
78 2015-05-04 4757.64 4787.74 4795.92 4699.40 377843853.0 sz399300 
79 2015-05-05 4785.19 4596.84 4785.19 4572.98 460419626.0 sz399300 

[80 rows x 7 columns] 

回答

2

只是做df.index = pd.RangeIndex(0, df.shape[0])

直接覆蓋索引,以reindex它有效地保持現有的索引並返回你,對應於行值傳遞的行,因爲它們不存在,則顯示NaN

實施例:

In[92]: 
df = pd.DataFrame(np.random.randn(5,3), columns = list('abc'), index=[3,4,5,10,50]) 
df 

Out[92]: 
      a   b   c 
3 -0.185420 0.230181 1.561401 
4 -0.142055 -1.130427 -1.209588 
5 2.590563 0.367157 1.878946 
10 0.317735 -1.578927 0.555270 
50 1.424068 0.667701 0.619741 

In[93]: 
df.index = pd.RangeIndex(0,df.shape[0]) 
df 

Out[93]: 
      a   b   c 
0 -0.185420 0.230181 1.561401 
1 -0.142055 -1.130427 -1.209588 
2 2.590563 0.367157 1.878946 
3 0.317735 -1.578927 0.555270 
4 1.424068 0.667701 0.619741 

這裏RangeIndex是單調int指數的優化索引對象,您也可以執行df.index = np.arange(0, df.shape[0]),但是這會創建一個全範圍的np數組,它無論如何都會被轉換爲RangeIndex。隨着RangeIndex它只是需要知道的啓動,停止範圍和步驟,它的內存使用效率,

或者就叫reset_index(drop=True)

In[94]: 
df = df.reset_index(drop=True) 
df 

Out[94]: 
      a   b   c 
0 -0.185420 0.230181 1.561401 
1 -0.142055 -1.130427 -1.209588 
2 2.590563 0.367157 1.878946 
3 0.317735 -1.578927 0.555270 
4 1.424068 0.667701 0.619741 

這將從根本上刪除當前指數與該指數開始改造它從0

+0

哇,太酷了。我特別喜歡這個選擇:)。 – jetorz

相關問題