Issue
I have 3 basic problems when displaying things in IPython / JupyterLab.
(1) I have a pandas dataframe with many columns. First, I make sure I can see a decent portion of it:
import numpy as np
import pandas as pd
np.set_printoptions(linewidth=240,edgeitems=5,precision=3)
pd.set_option('display.width',1800) #number of pixels of the output
pd.set_option('display.max_columns',100) #replace the number with None to print all columns
pd.set_option('display.max_rows',10) #max_columns/max_rows sets the maximum number of columns/rows displayed when a frame is pretty-printed
pd.set_option('display.min_rows',9) #once max_rows is exceeded, min_rows determines how many rows are shown in the truncated representation
pd.set_option('display.precision',3) #number of digits in the printed float number
If I print it, everything gets mushed together: enter image description here Is it possible to print text wide, i.e. in a way that each line (even if longer) is printed on only 1 line in output, with a slider when lines are wider than the window?
(2) If I display the mentioned dataframe, it looks really nice (has a slider), but some string entries are displayed over 4 rows: enter image description here
How can I make sure every entry is displayed in 1 row?
(3) The code below produces the output, which works fine:
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0.1,30,1000);
fig,ax=plt.subplots(1, 4, constrained_layout=True, figsize=[15,2])
ax=ax.ravel()
ax[0].plot( x, np.sin(x))
ax[1].plot( x, np.log(1+x))
ax[2].plot( x, np.sin(30/x))
ax[3].plot( x, np.sin(x)*np.sin(2*x))
plt.show()
enter image description here
However, when I change [15,2]
to [35,2]
, the figure will only be as wide as the window. How can I achieve that larger widths produce a slider (like display of a dataframe) so that I can make images as wide as I wish?
Solution
You solved (1) already by deciding to display the dataframe with the method in (2). Using print
to display a dataframe is not very useful in my opinion.
(2): The display(df)
automatically utilizes white spaces to wrap cell content. I did not find a pandas option to disable this behavior. Luckily, someone else had the same problem already and another person provided a solution.
You have to change the style properties of your dataframe. For this you use the Styler
, which holds the styled dataframe. I made a short example from which you can copy the line:
import pandas as pd
# Construct data frame content
long_text = 'abcabcabc ' * 10
long_row = [long_text for _ in range(45)]
long_table = [long_row for _ in range(15)]
# Create dataframe
df = pd.DataFrame(long_table)
# Select data you want to output
# df_selected = df.head(5) # First five rows
# df_selected = df.tail(5) # Last five rows
df_selected = df.iloc[[1,3, 7]] # Select row 1,3 and 7
# Create styler of df_selected with changed style of white spaces
dataframe_styler = df_selected.style.applymap(lambda x:'white-space:nowrap')
# Display styler
display(dataframe_styler)
(3) As I already mentioned in the comment, you simply have to double click onto the diagram and it is displayed with a slider.
Answered By - JANO
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.