Python pandas display dataframe as a table with tabs and not spaces - python

How can I print or display a pandas.DataFrame object like a "real" table? I mean using only one tab between columns and not more spaces. In IPython Jupyter Notebook I can use the following code to get a "real" table style:
from IPython.core.display import display
display(df.head(50))
instead of print(df.head(50)) which uses spaces.
Is there any same in IPython console using Spyder? I did no find a proper pd.set_option() value...

Unfortunately the Jupyter Notebook is a specialized environment, which knows how to tell objects to render themselves as HTML inside the Jupyter (web) interface. I don't believe Spyder has this capability.

Related

View dataframe while debugging in VS Code

I'm trying to explore switching from PyCharm to VS Code. I can't find a way right now to view my pandas DataFrames in a tabular format while debugging.
When I right click on a df object, there is no option to view.
I have the python extension downloaded. Am I missing something?
Microsoft VSCode team finally made this feature available with latest update of the product. More details could be found in official blog
It works like a charm and is very intuitive. In short:
Set up a break point (by clicking at the left most point of code area, before line number)
Start debugging (Run menu at top have Start Debugging option)
When debugger stops at the debug point, find the required dataframe inside VARIABLES panel. (VARIABLES panel is inside Run and Debug area)
Right click on dataframe and select option View Value in Data Viewer. TADA :)
You can now print the DataFrame in the DEBUG CONSOLE:
From the Github issue mentioned in #Christina Zhou's answer.
My solution for viewing DataFrames in a tabular format while debugging is to simply copy and paste them into an Excel spreadsheet using
df.to_clipboard()
from the debug console. Even some of my colleagues running PyCharm are using this technique, since it gives you way more flexibility to inspect your data.
It seems like currently you can do it only using the Jupyter notebook in VS Code, using the variables explorer.
So it looks like this isn't a thing right now in VS Code.
If anyone wants to show their support for the development of this feature, I found this open issue here:
https://github.com/microsoft/vscode-python/issues/7063
you can use the view() function from xlwings library. It will show you the DataFrame in Excel:
import pandas as pd
from xlwings import view
df = pd.DataFrame({'A':[1,2], 'B':[3,4]})
view(df)
A better way would be to convert the function to pandas method:
from pandas.core.base import PandasObject
PandasObject.view = view
now you only need to type:
df.view()
Two more options for vscode are the following ones:
jupyter notebooks
saving to CSV and using the edit csv extension
Both require more effort but the view is more helpful.
The interactive shell looks like a good start. Right click the .py file in your explorer. You'll be able to view pandas dataframes from there.

Interactive multi chart widget in jupyter notebook for pandas dataframe

When using sparkmagic in Jupyter, it generates this interactive visualization of a dataframe. (full example at from https://github.com/jupyter-incubator/sparkmagic/blob/master/examples/Pyspark%20Kernel.ipynb). How to achieve the same visualization controls for a normal python notebook having pandas dataframe objects?
No matter what I install the dataframe when viewed in a cell, shows up only as a table.
Posted the same question on their github page - https://github.com/jupyter-incubator/sparkmagic/issues/478
#apetresc helped me with this link - https://github.com/jupyter-incubator/sparkmagic/blob/master/autovizwidget/examples/autoviz.ipynb
Idea is to use autovizwidget library like this,
from autovizwidget.widget.utils import display_dataframe
display_dataframe(df)

How to prevent charts or tables to disappear when I re-open Jupyter Notebook?

I use Pandas with Jupyter notebook a lot. After I ingest a table in from using pandas.read_sql, I would preview it by doing the following:
data = pandas.read_sql("""blah""")
data
One problem that I have been running into is that all my preview tables will disappear if I reopen my .ipynb
Is there a way to prevent that from happening?
Thanks!
Are you explicitly saving your notebook before you re-open it? A Jupyter notebook is really just a large json object, eventually rendered as a fancy html object. If you save the notebook, illustrations and diagrams should be saved as well. If that doesn't do the trick, try putting the one-liner "data" in a different cell than read_sql().

Is it possible to redirect cell output in jupyter

I am using jupyter and jupyter-nbconvert to create a html presentation. However, I have some cells that produce an output image that I want to share on a separate slide. Is it possible to redirect the output of one cell to its own slide?
You might want to consider using Damian Vila's Jupyter extension RISE. It provides some of the control you need for how cells are displayed in slides.
It is flagged by the latest Jupyter (3.6) as possibly not compatible, but I've seen no problems using it so far.

Create a view Table in python

I would like to know is there any widget like c# (Data Grid View) that displays the data in row and column?
I retrieve data from postgreSQL
The pandas library (http://pandas.pydata.org/), specifically the DataFrame. You can display the table in multiple formats, from HTML (if you're using IPython Notebook, for example) to LaTeX via the functions to_html() or to_latex(), as well as a tabbed printing for working from a console. It does not give you interactive editing within the display, but you can view any portion of the data. It can also read sql directly into a Data Frame.

Categories

Resources