2017-09-19 262 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