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).
Related
Using VSCode with Pylance, creating a basic series with pandas shown here will show an error. I looked around online and this question hasn't been asked yet, so I'm assuming I have some basic setup done incorrectly.
Using conda, python#3.8.3, pandas#1.4.4
import pandas as pd
test_series = pd.Series([1, 3, 5, 6, 7, 8])
(variable) test_series: Series[Unknown]
Type of "test_series" is partially unknown
Type of "test_series" is "Series[Unknown]"PylancereportUnknownVariableType
Using PyCharm 2022.1.4 to develop Jupyter notebooks, I would like to see complete Pandas dataframes without vertical scrollbars. Using the following code, the entire dataframe can be seen if scrolled. I would rather have all the lines shown at once. Thank you
from IPython.core.display_functions import display
import numpy as np
with pd.option_context('display.max_rows', None, 'display.max_columns', None, "expand_frame_repr", False):
display(pd.DataFrame(np.random.randint(0,100,size=(15, 40))))
The question:
Can I ignore or prevent the SettingWithCopyWarning to be printed to the console using warnings.simplefilter()?
The details:
I'm running a few data cleaning routines using pandas, and those are executed in the simplest of ways using a batch file. One of the lines in my Python script triggers the SettingWithCopyWarning and is printed to the console. But it's also being echoed in the command prompt:
Aside from sorting out the source of the error, is there any way I can prevent the error message from being printed to the prompt like I can with FutureWarnings like warnings.simplefilter(action = "ignore", category = FutureWarning)?
Though I would strongly advise to fix the issue, it is possible to suppress the warning by importing it from pandas.core.common. I found where it's located on GitHub.
Example:
import warnings
import pandas as pd
from pandas.core.common import SettingWithCopyWarning
warnings.simplefilter(action="ignore", category=SettingWithCopyWarning)
df = pd.DataFrame(dict(A=[1, 2, 3], B=[2, 3, 4]))
df[df['A'] > 2]['B'] = 5 # No warnings for the chained assignment!
You can use:
pd.set_option('mode.chained_assignment', None)
# This code will not complain!
pd.reset_option("mode.chained_assignment")
Or if you prefer to use it inside a context:
with pd.option_context('mode.chained_assignment', None):
# This code will not complain!
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?
A generalized recreation of the table I'm having issues with, which will come out being a table with 12000 rows and a MultiIndex. The big issue: I can't get this table to display in a rendered version of a Jupyter Notebook.
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(12000, 6), columns=list('ABCDCD'))
second_cols = ([''] * 2) + ['X', 'X', 'Y', 'Y']
df.columns = pd.MultiIndex.from_arrays([df.columns,second_cols])
df = df.swaplevel(0,1,1).sort_index(1)
df
My company uses proprietary software that lets us turn an .ipynb file into a interactive report. However, most of my issues are Jupyter notebook-level. Here's what I've tried and what's happened:
Just display df: The tables print out, but only show rows [0:29, 19930:12000] and there is no sorting available.
display(HTML(df.to_html())): gives me the following error:
IOPub data rate exceeded.
The notebook server will temporarily stop sending output
to the client in order to avoid crashing it.
To change this limit, set the config variable
--NotebookApp.iopub_data_rate_limit.
DataViz and GoogleCharts do not support MultiIndexes.
Any ideas to make my dataframe render?! Perhaps packages that can paginate my table, etc? Note the big issue here is the MultiIndex.