How to increase Google Colab cell output width? - python

I found a very similar question but the solution did not work on Google colab. I would like to see better the text in my pandas dataframes columns. Right now the default width seems 50%.
from IPython.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
display(HTML("<style>.output_result { max-width:100% !important; }</style>"))
As an alternative, I found an extension called Data Tables. This may be the best solution available today for it but seems to only works for tables.
from google.colab import data_table
data_table.enable_dataframe_formatter()

Related

Plotly Python: Export image with different font styles

Problem Summary
I'm trying to export a plotly figure with multiple fonts / font stylings as a .png file.
Within the browser, the figure renders correctly. The y-axis has different font style that the rest of the figure.
Both fonts are installed on my system.
But if I try to export the image as a png-file, the different font styling for the y-axis seems to disappear.
Minimum code example
`
import pandas as pd
import plotly.express as px
df = pd.read_csv("https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv")
fig = px.histogram(df, x="Age", color="Sex")
fig.update_layout(font_family="Arial")
fig.update_yaxes(tickfont_family="Arial Black")
fig.show()
fig.write_image("test_image.png")
`
My issue might be related to this post. But both fonts are installed on my machine.
Does anyone know how to include multiple fonts in a plotly figure export?
It seems that the issue has to do with kaleido:0.2.1 when installed via conda.
If kaleido:0.2.1 is installed via pip, the export of images with different font styles works just fine.

Pandas on Jupyter Notebook VS Code not displaying outputs properly

Does anyone know how to make Pandas outputs display properly in VS Code Jupyter Notebooks?
As you can see in the image, the values for individual columns in the output of a Pandas DataFrame aren't aligned (whereas they are aligned if you use Jupyter Notebooks directly on a web browser).
Click on the 3 dotted lines highlighted in the image above. From there its going to prompt you to select text or html. Click HTML, and the full data frame should show up
For the alignment of pandas, I think I have a relatively simple strategy. We can use IPython module.Use the following code to output the results.
from IPython.display import display
display(df2)
I think you've got a bracket mismatch. Change your code to this:
df2.loc[df2['Airport'] == 'ATL', ['Delay']]

Python/R Code for creating a schema as shown below?

I need to prepare a set of flowcharts that look like this:
The goal here is to show dataset splits for training models, where the different colors show different categorical variables in the y-parm.
What is important here is that the ratio be perfectly preserved (80:20 here).
Please help me with the code to do so (preferably R).
I tried using DiagrammeR, but I lack a coding background and was thus unable to set the bar widths accurately.
Edit: As asked, here's my (admittedly poor) code:
library(DiagrammeR)
grViz("digraph flowchart {
node [shape=rectangle, height=0.75, color=grey]
ds1[label='Dataset-1', width=5]
ds1tr[label='Training Set-1', width=4]
ds1ts[label='Test Set-1', width=1]
ds1->ds1tr;
ds1->ds1ts
}")
I need help to-
Position each node properly
Fix the colors in the proper ratio (9:1/8:2)
Beautify the arrows/edges
Thank you in advance.

Why does pandas hide columns during display? [duplicate]

I have a simple csv file with ten columns!
When I set the following option in the notebook and print my csv file (which is in a pandas dataframe) it doesn't print all the columns from left to right, it prints the first two, the next two underneath and so on.
I used this option, why isn't it working?
pd.option_context("display.max_rows",1,"display.max_columns",100)
Even this doesn't seem to work:
pandas.set_option('display.max_columns', None)
I assume you want to display your data in the notebook than the following options work fine for me (IPython 2.3):
import pandas as pd
from IPython.display import display
data = pd.read_csv('yourdata.txt')
Either directly set the option
pd.options.display.max_columns = None
display(data)
Or, use the set_option method you showed actually works fine as well
pd.set_option('display.max_columns', None)
display(data)
If you don't want to set this options for the whole script use the context manager
with pd.option_context('display.max_columns', None):
display(data)
If this doesn't help, you might give a minimal example to reproduce your issue.
You can also display all the data by asking pandas to return HTML markup, and then having IPython render the HTML table.
import pandas as pd
from IPython.display import HTML
data = pd.read_csv('yourdata.csv')
HTML(data.to_html())
Using IPython 3.0.0 and Python 3.4, I found that display(data) as described by #Jakob will render as a table with up/down and left/right scroll bars, but the table is still wider than the cell and some columns are off-screen to the right. To see all the data, one must collapse the cell - which adds scroll bars. Consequently you have a scrolling box in a scrolling box, which is not ideal as you have to shift focus between the doubled-up scroll bars to navigate all the way through the data.
Using the HTML method, you render the enormous table as-is without any scroll bars. This cell can then be collapsed down to show only a single vertical and horizontal bar, which is more user-friendly.
The caveat to using HTML is the table takes longer to render. I was only using a ~150x50 matrix and the speed difference was noticeable, but not inconvenient. If you have an enormous table, don't use this method to display the entire thing at once. That said, if you do have an enormous table, rendering the whole thing at once is obviously going to be a bad idea however you try to do it.
I found this question as one of the first hits on Google. In jupyter lab,
pandas.set_option("display.max_columns", None)
Now seems to work fine - my example was 32 columns, it used to be truncated and is not any more.

How to display two local images side by side in jupyter?

I want to display two images, that I have them saved locally, in one single makedown cell side-by-side? I found previous post but it uses HTML() not Image() method.
The way I tried was:
from IPython.core.display import Image, display
display(Image('/whateverfile/counts1.png',width=100,height=100), Image('/whateverfile/counts2',width=100, height=100))
But that did not work.
Any ideas how to solve this?
Thanks
from IPython.display import HTML, display
display(HTML("<table><tr><td><img src='/image/counts1.png'></td><td><img src='/image/counts2'></td></tr></table>"))
Set other parameters as you like.
Or maybe using nbextensions and splitting the cell in two (having 2 outputs side by side) would fit your needs?

Categories