I just started using jupyter cells in Visual Studio Code through the Python extension. It is outputting plots fine, but my dataframe is not showing up like the blog example from Microsoft. Below is my code I am running in VS Code:
#%%
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import pandas as pd
x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.show()
#%%
d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)
df
My output looks like this:
VS Code Cell outputs
I am really excited to use jupyter in VS Code but I need to view the dataframes like in other variable explorers.
I am on windows using Anaconda as my environment.
jupyter=1.0.0=py36_7
jupyter_client=5.2.3=py36_0
jupyter_console=6.0.0=py36_0
jupyter_core=4.4.0=py36_0
numpy=1.15.4=py36h19fb1c0_0
pandas=0.23.4=py36h830ac7b_0
I uninstalled my Anaconda 3.6 and installed the newer Anaconda 3.7 and now it works in VS Code.
That error means we don't have the capability to render the df output for some reason.
The only thing I can think of is you might have a jupyter extension that's modifying the result of a df. (normally it returns an html table to us)
Do you know what jupyter extensions you have installed?
Related
I'm trying to create a hist plot in Power BI.
I got installed ANaconda, MS Vusial Code.
Screenshots with my settings:
I'm trying make hist with simple table with 1 column.
The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:
dataset = pandas.DataFrame(reg_min_ses_dt)
dataset = dataset.drop_duplicates()
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.histplot(data=dataset['reg_min_ses_dt'])
plt.show()
But I get this error:
I think, I just didn't set up some python extension or something else.
I just want make Python visual like this.
You need to activate conda before you can use its packages in PBIDesktop.exe. Simple as that.
I need to display a single-row dataframe:
import pandas as pd
from IPython.display import display, HTML
stack = [42, 27, 13]
df = pd.DataFrame([stack], columns=[1, 2, 3], index=["stack"])
display(HTML(df.to_html())) # or simply: display(df)
Is it possible to get rid of the sorting arrows, which are useless here?
Based on the discussion we had in the comment. You had a Jupyter extension installed on your system (called table_beautifier).
Disabling this should get the expected result (without sorting arrows).
A few days back, accidentally, I was able to plot technical indicators using these codes
ch.add_BBANDS()
ch.add_RSI(14)
But now I am not able to do so as I am having a problem installing quantmod.
I know we can plot candlestick chart by using plotly but with quantmod there seems to be a single line code which would be easier.
I realized that the problem is in Installing and importing quantmod as it says:
ImportError:
The plotly.plotly module is deprecated,
please install the chart-studio package and use the
chart_studio.plotly module instead.
I also tried to degrade my plotly version as mentioned here but nothing worked. I want to know if someone has been able to install and import quantmod in jupyter notebook and plot some of the charts as indicated by following codes. I would appreciate if you show me the correct way to get quantmod working in my jupyter notebook.
ch = qm.Chart(df)
ch.to_figure(type='ohlc', dimensions=(2560,1440))
ch = qm.Chart(df)
ch.add_BBANDS()
ch.add_RSI(14)
ch.to_figure(type='candlestick', title='EQUITY')
Try using QuantFig to get what you are trying to get. Chart studio now requires a sign in. QuantFig uses the index of the pandas dataframe as the x axis for the chart. So take care to format it. the columns shall be named 'open', 'high', 'low', close' as it is expected by QuantFig. you can use the following snippet for inspiration.
import chart_studio.plotly as py
import cufflinks as cf
import python as pd
py.sign_in('Python-Demo-Account', 'gwt101uhh0')
def plotData(df=None):
if df == None:
df=cf.datagen.ohlc()
qf=cf.QuantFig(df,title='First Quant Figure',legend='top',name='GS')
qf.add_bollinger_bands()
qf.add_rsi()
fig = qf.iplot(asFigure=True)
fig.show()
plotData()
Is it possible to run R and Python code in the same Jupyter notebook. What are all the alternatives available?
Install r-essentials and create R notebooks in Jupyter.
Install rpy2 and use rmagic functions.
Use a beaker notebook.
Which of above 3 options is reliable to run Python and R code snippets (sharing variables and visualizations) or is there a better option already?
Yes, it is possible! Use rpy2.
You can install rpy2 with: pip install rpy2
Then run %load_ext rpy2.ipython in one of your cells. (You only have to run this once.)
Now you can do the following:
Python cell:
# enables the %%R magic, not necessary if you've already done this
%load_ext rpy2.ipython
import pandas as pd
df = pd.DataFrame({
'cups_of_coffee': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
'productivity': [2, 5, 6, 8, 9, 8, 0, 1, 0, -1]
})
R cell:
%%R -i df -w 5 -h 5 --units in -r 200
# import df from global environment
# make default figure size 5 by 5 inches with 200 dpi resolution
install.packages("ggplot2", repos='http://cran.us.r-project.org', quiet=TRUE)
library(ggplot2)
ggplot(df, aes(x=cups_of_coffee, y=productivity)) + geom_line()
And you'll get your pretty figure plotting data from a python Pandas DataFrame.
You also have access to R objects (e.g. data frames) from Python cells:
import rpy2.robjects as robjects
robjects.globalenv['some-variable-name']
To view the names of all available variables use:
list(robjects.globalenv.keys())
Details are explained here: Pandas - how to convert r dataframe back to pandas?
Using #uut's answer for running R in a jupyter notebook within python kernel (in MacOS), the following worked for me.
%%Rshould always be at the start of the cell else you will get the error as shown in figure below
The following is the right way:
Also %load_ext rpy2.ipython should come before %%R hence put it in a different cell above it as shown in the figures.
UPDATE April 2018,
RStudio has also put out a package:
https://blog.rstudio.com/2018/03/26/reticulate-r-interface-to-python/
for which it is possible to run multiple code chunks in different languages using the R markdown notebook, which is similar to a jupyter notebook.
In my previous post, I said that the underlying representation of objects is different. Actually here is a more nuanced discussion of the underlying matrix representation of R and python from the same package:
https://rstudio.github.io/reticulate/articles/arrays.html
Old post:
It will be hard for you to use both R and Python syntax in the same notebook, mostly because the underlying representation of objects in the two languages are different. That said, there is a project that does try to allow conversion of objects and different languages in the same notebook:
http://beakernotebook.com/features
I haven't used it myself but it looks promising
SoS kernel is another option.
Don't know how well it performs yet, just started using it.
The SoS kernel allows you to run different languages within the same notebook, including Python and R.
SoS Polyglot Notebook - Instructions for Installing Desired Languages
Here is an example of a notebook with Python and R cells.
*Update:
In terms of sharing variables, one can use the magics %use and %with.
"SoS automatically shares variables with names starting with sos among all subkernels"1.
Ex.
Starting cell in R:
%use R
sos_var=read.csv('G:\\Somefile.csv')
dim(sos_var)
Output:
51 13
Switching to python:
%with Python3
sos_var.shape
Output:
(51, 13)
A small addition to #uut's answer and #msh's comment:
If you are using rpy2 in Jupyter Notebooks you also have access to R objects (e.g. data frames) from Python cells:
import rpy2.robjects as robjects
robjects.globalenv['some-variable-name']
To view the names of all available variables use:
list(robjects.globalenv.keys())
Details are explained here:
Pandas - how to convert r dataframe back to pandas?
I have been trying to create a Vincent time series line plot. My code is as follows:
#!/usr/bin/env python
import pandas as pd
import numpy as np
import vincent
#test data
df2 = pd.DataFrame({ 'A' : 1., 'B' : pd.Timestamp('20130102'),'C' : pd.Series(1,index=list(range(4)),dtype='float32'),'D' : np.array([3] * 4,dtype='int32'), 'E' : pd.Categorical(["test","train","test","train"]), 'F' : 'foo' })
vis = vincent.Line(df2) # test
vis.axis_titles(x='Time', y='Freq')
vis.legend(title='Words')
vis.to_json('chart.json')
vis.display()
I get no output (no display or chart.json created) or any errors. The other similar problems on here are due to Ipython notebook or Canopy issues, for example this; I am not using Ipython, notebook or Canopy. My question is: why is there no json created?
EDIT: OK maybe I am using Ipython without knowing it! I get this output:
<IPython.core.display.HTML at 0x7f980791e2d0>
However adding vis.core.initialize_notebook()from this solution does not help.
It was an issue with Eclipse. My working directory was for some reason set to a subdirectory where the json file WAS being created. I post this in case someone else is as stupid as me.
EDIT: I added the Eclipse tag. Should have been there in the beginning.