head() prints the indexes.
dataframe.to_string(index=False,max_rows=10) prints the first 5 and last 5 rows.
Asked
Active
Viewed 3.0k times
4
MarianD
- 13,096
- 12
- 42
- 54
CornyBunches
- 41
- 1
- 1
- 2
-
print(ndf.head().style.hide_index()) just prints the following:
– CornyBunches Feb 22 '20 at 22:12 -
**Omit `print()`**, use **only** `ndf.head().style.hide_index()`. — You have to work in Jupyter Notebook (or JupyterLab) environment. See [my answer](https://stackoverflow.com/a/60357424/7023590). – MarianD Feb 22 '20 at 22:49
2 Answers
5
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 ^^.
Omar Aldakar
- 505
- 2
- 8
2
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.
MarianD
- 13,096
- 12
- 42
- 54