python提取前几行数据_获取datafram的前几行

In [7]:

df = pd.DataFrame(np.random.randn(10,3))

df

Out[7]:

0 1 2

0 -1.230919 1.482451 0.221723

1 -0.302693 -1.650244 0.957594

2 -0.656565 0.548343 1.383227

3 0.348090 -0.721904 -1.396192

4 0.849480 -0.431355 0.501644

5 0.030110 0.951908 -0.788161

6 2.104805 -0.302218 -0.660225

7 -0.657953 0.423303 1.408165

8 -1.940009 0.476254 -0.014590

9 -0.753064 -1.083119 -0.901708

In [8]:

df.head(5)

Out[8]:

0 1 2

0 -1.230919 1.482451 0.221723

1 -0.302693 -1.650244 0.957594

2 -0.656565 0.548343 1.383227

3 0.348090 -0.721904 -1.396192

4 0.849480 -0.431355 0.501644

In [11]:

df.iloc[:5]

Out[11]:

0 1 2

0 -1.230919 1.482451 0.221723

1 -0.302693 -1.650244 0.957594

2 -0.656565 0.548343 1.383227

3 0.348090 -0.721904 -1.396192

4 0.849480 -0.431355 0.501644