2017-09-19 256 views
-1

假設一個熊貓的數據幀的樣子:如何提取熊貓數據框的第n行作爲熊貓數據框?

X_test.head(4) 
    BoxRatio Thrust Velocity OnBalRun vwapGain 
5  -0.163 -0.817  0.741  1.702  0.218 
8  0.000 0.000  0.732  1.798  0.307 
11  0.417 -0.298  2.036  4.107  1.793 
13  0.054 -0.574  1.323  2.553  1.185 

我怎樣才能提取第三行(如ROW3)作爲PD數據幀? 換句話說,row3.shape應該是(1,5)和row3.head()應爲:

0.417 -0.298  2.036  4.107  1.793 
+1

你有https://stackoverflow.com/questions/16096627/pandas-select-row-of-data-frame-by-integer-index嗎? – Zero

+0

其實0已經找到了詳細的相應答案 – bellum

+0

可能重複[Pandas選擇整行索引數據幀的行](https://stackoverflow.com/questions/16096627/pandas-select-row-of-data-frame-by-整數索引) – bellum

回答

3

使用.iloc雙括號以提取數據幀,或單括號拉出的索引。

X_test.iloc[[2]] # DataFrame result 
    BoxRatio Thrust Velocity OnBalRun vwapGain 
idx             
11  0.417 -0.298  2.036  4.107  1.793 

X_test.iloc[2] # Series result 
BoxRatio 0.417 
Thrust  -0.298 
Velocity 2.036 
OnBalRun 4.107 
vwapGain 1.793 
Name: 11, dtype: float64