打印每个第n行的pandas数据帧

时间:2016-07-21 12:50:12

标签: python pandas printing

是否有优雅的解决方案只打印pandas数据帧的每个第n行?例如,我想只打印第2行。

这可以通过

完成
i = 0
for index, row in df.iterrows():
    if ((i%2) == 0):
        print(row)
    i++

但有更多的pythonic方法吗?

1 个答案:

答案 0 :(得分:4)

使用带iloc的步骤参数切片df:

print(df.iloc[::2])

In [73]:
df = pd.DataFrame(np.random.randn(5,3), columns=list('abc'))
df

Out[73]:
          a         b         c
0  0.613844 -0.167024 -1.287091
1  0.473858 -0.456157  0.037850
2  0.020583  0.368597 -0.147517
3  0.152791 -1.231226 -0.570839
4 -0.280074  0.806033 -1.610855

In [77]:
print(df.iloc[::2])

          a         b         c
0  0.613844 -0.167024 -1.287091
2  0.020583  0.368597 -0.147517
4 -0.280074  0.806033 -1.610855
相关问题