head()
打印索引。dataframe.to_string(index=False,max_rows=10)
打印前5行和后5行。
您应该尝试此:
print(df.head(n=10).to_string(index=False))
这将起作用,因为df.head
返回一个Dataframe
对象,因此您可以对其应用to_string
方法并摆脱该索引^^。
如果您喜欢复杂的解决方案,则可以使用
[print(row) for row in df.head().to_string(index=False).split("\n")]
说明:
df.head().to_string(index=False)
返回一个以"\n"
作为行分隔符的字符串,split()
方法然后返回单行的列表,[print(row) for row in ...]
然后打印每一行。这是个玩笑,尽管给了您想要的结果。将其打印为整个字符串将得到相同的结果(每行以"\n"
结尾):
print(df.head().to_string(index=False))
如果使用Jupyter Notebook,则可以使用更好的命令
df.head().style.hide_index()
小心!
没有print(...)
,没有df = ...
。返回的对象是Styler
类的对象,not一个数据框。
[Jupyter Notebook IDE 自动调用其方法._repr_html()
渲染(显示)表。
有关详细信息,请参见my other answer。