Can't display Latex expressions in pandas using Google collab - python

Hello I have a problem that I cant display a simple pandas table whitch has Latex expressions in it.
I have a simple python script:
x = 1;
x1 = 2;
my_dict = {'Case1':{'Formula1':'$$x^{-1}$$', 'Formula2':'$$x^2$$', 'Formula3':x},
'Case2':{'Formula1':'$$x^{-2}$$', 'Formula2':'$$x^4$$', 'Formula3':x1}}
df = pd.DataFrame(my_dict)
display(df.transpose())
When I am using Jupyter Notebook it works fine like this:
Example in Jupyter Notebook
but when I opened the same code in Google collab it is showing like this:
Formula1 Formula2 Formula3
Case1 $$x^{-1}$$ $$x^2$$ 1
Case2 $$x^{-2}$$ $$x^4$$ 2
Is there something I can do to make Google collab display it like Jupyter notebook ?

You could use something like:
from IPython.display import Math
Math('$$x^{-1}$$')
This wouldn't work for the cells inside the table, but I don't think there's any way to override pandas' behavior, so you might have to write your own code to print the table out with the formulas intact.
You could also do
import pandas as pd
from IPython.display import Markdown
v = pd.DataFrame({'Case1':{'Formula1':'$$x^{-1}$$', 'Formula2':'$$x^2$$', 'Formula3':1},
'Case2':{'Formula1':'$$x^{-2}$$', 'Formula2':'$$x^4$$', 'Formula3':2}})
display(Markdown(str(v)))
but the table formatting is broken.

Related

How can I configure a line chart using pivot_ui?

I would like to create a dynamic line chart in Voila. How can I manipulate the below code to show a standard line graph where the x axis equals column "a" and the y axis equals column "b"? Potentially the user can then dynamically update the output to make the y axis equal to column "c" by drag and drop etc.
from pivottablejs import pivot_ui
import pandas as pd
import IPython
df = pd.DataFrame(("a": [1,2,3], "b": [30,45,60],"c": [100,222,3444]))
display.display(df)
pivot_ui(df,outfile_path='pivottablejs.html',
rendererName="Line Chart",
cols= ["b","c"]
rows= ["a"],
aggregatorName="Sum"
)
display.display(IPython.display.HTML('pivottablejs.html"))
Thank you.
This should be a comment but I cannot post code with comments easily.
Please always test your code, preferably in a new notebook so it is fresh kerenl, before you post it.
Your dataframe assignment won't work. Should it be something like below?
That display code won't work in Jupyter notebook classic or JupyterLab presently.
Try something like this for assignment and display:
import pandas as pd
df = pd.DataFrame({"a": [1,2,3], "b": [30,45,60],"c": [100,222,3444]})
display(df)
That works in the classic notebook, JupyterLab, and Voila to make & display the dataframe.
Related to this is that is advisable to develop for Voila in JupyterLab. JupyterLab's rendering machinery is more modern and so closer to what Voila uses.
You can easily test renderings in launches from the Voila binder example page. Go there and click 'launch binder for the appropriate rendering. From JupyterLab you can select the Voila icon from the toolbar just above an open notebook and get the Voila rendering on the side.

How to display 11 variables of descriptive statistic on the same line

I'm trying to display the Descriptive statistic of a data frame with 12 variables.For readability my goal is to have all the variables on the same line.The following is my code
# Descriptive statistic
from pandas import read_csv
from pandas import set_option
filename = 'winequality-red.csv'
data = read_csv(filename,sep = ';')
set_option('display.max_columns',500)
descriptions = data.describe()
print(descriptions)
the output is giving me 2 lines for the variables
Please I need help to achieve my goal
thanks
In case of you are using IDE, or notebook, it's going to hurt the readability, and never help.
Anyway, it's could be done by using transpose descriptions.T.
If you are using IDE you could use IPython.display instead of print
from IPython.display import display
display(descriptions) # OR - display(descriptions.T)

Using python and R in Jupyter notebook at the same time

I am using Python and R code with jupyter notebook at the same time. Specifically, I want to use pandas to deal with the data, pass the DataFrame object to R kernal, and then use ggplot2 to visualize it.
However, as long as I pass the pandas DataFrame object to the R kernal, and use ggplot() to make plots,the jupyter notebook will always give a warning as following:
C:\Study\Anaconda3-5.2.0\lib\site-packages\rpy2-2.9.4-py3.6-win-amd64.egg\rpy2\robjects\pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order.
res = PandasDataFrame.from_items(items)
My code is very simple, showing as the following:
%load_ext rpy2.ipython
%R library(ggplot2)
# data_train is a pandas DataFrame object
%%R -i data_train
ggplot(data = data_train,aes(x = factor(Survived))) + geom_bar(fill = "#539bf3")
You could do it directly in python using python ggplot library
Not exactly what you are asking but in case you overlook it

IPython Jupyter Notebook Latex newpage

How do I insert a newpage/pagebreak into the pdf output of a Jupyter Notebook using the IPython.display.Latex function?
nbconvert is used:
nbconvert --to=pdf
A latex cell works fine:
%%latex
\newpage
but this doesn't do anything:
Latex(r"\newpage")
unless it's the last line of the cell (that's why it works when it's in its own cell), it should be:
display(Latex(r"\newpage"))
Further elaborating on the answer of #Julio, what you need to incorporate some latex code into a Code cell is:
from IPython.display import display, Math, Latex
then you can procedurally add Latex syntax or Math in any code cell like:
display(Latex(r"\newpage"))
for math formulas, swap Latex with Math.
This will work for scenarios where, e.g., you want to add one line break for each iteration of a for loop.

Import csv Python with Spyder

I am trying to import a csv file into Python but it doesn't seem to work unless I use the Import Data icon.
I've never used Python before so apologies is I am doing something obviously wrong. I use R and I am trying to replicate the same tasks I do in R in Python.
Here is some sample code:
import pandas as pd
import os as os
Main_Path = "C:/Users/fan0ia/Documents/Python_Files"
Area = "Pricing"
Project = "Elasticity"
Path = os.path.join(R_Files, Business_Area, Project)
os.chdir(Path)
#Read in the data
Seasons = pd.read_csv("seasons.csv")
Dep_Sec_Key = pd.read_csv("DepSecKey.csv")
These files import without any issues but when I execute the following:
UOM = pd.read_csv("FINAL_UOM.csv")
Nothing shows in the variable explorer panel and I get this in the IPython console:
In [3]: UOM = pd.read_csv("FINAL_UOM.csv")
If I use the Import Data icon and use the wizard selecting DataFrame on the preview tab it works fine.
The same file imports into R with the same kind of command so I don't know what I am doing wrong? Is there any way to see what code was generated by the wizard so I can compare it to mine?
Turns out the data had imported, it just wasn't showing in the variable explorer

Categories

Resources