Reputation: 61
head()
prints the indexes.
dataframe.to_string(index=False,max_rows=10)
prints the first 5 and last 5 rows.
Upvotes: 6
Views: 40635
Reputation: 14151
If you like complicated solutions, you may use
[print(row) for row in df.head().to_string(index=False).split("\n")]
The explanation:
df.head().to_string(index=False)
returns a string with "\n"
as row delimiters,split()
method then returns a list of single rows,[print(row) for row in ...]
then prints every row.It was a joke, of course, albeit giving you the desired result. Printing it as a whole string will give the same result (as every row ends with "\n"
):
print(df.head().to_string(index=False))
If you work with Jupyter Notebook, you may use a nicer command
df.head().style.hide_index()
Be careful!
No print(...)
, no df = ...
. The returning object is an object of the Styler
class, not a dataframe.
Jupyter Notebook IDE automatically calls its method ._repr_html()
to render (display) your table.
See my other answer for details.
Upvotes: 2
Reputation: 545
You should try this :
print(df.head(n=10).to_string(index=False))
This will work because df.head
return a Dataframe
object so you can apply the to_string
method to it and get rid of that index ^^.
Upvotes: 9