Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Enhance printing options for Styler: head and tail ? #42533

Closed
dennymarcels opened this issue Jul 14, 2021 · 10 comments
Closed

ENH: Enhance printing options for Styler: head and tail ? #42533

dennymarcels opened this issue Jul 14, 2021 · 10 comments
Labels
Enhancement IO HTML read_html, to_html, Styler.apply, Styler.applymap Styler conditional formatting using DataFrame.style

Comments

@dennymarcels
Copy link

Is your feature request related to a problem?

More like an improvement.

Describe the solution you'd like

I just started playing around with pandas styling, and I noticed there is no way to print only the head and/or tail of a stylized DataFrame. It would be awesome if the styling could be applied to the whole df but then we could print only selected rows, like head and/or tail.

API breaking implications

I guess there could be a parameter passed to methods such as bar, or a new method to be appended to the end of a call.

Describe alternatives you've considered

I can apply the style to a slice of a DataFrame but then the styling function is applied to this slice only. What if I want to highlight_max to the whole df but print head/tail only?

@dennymarcels dennymarcels added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 14, 2021
@attack68
Copy link
Contributor

attack68 commented Jul 14, 2021

Interesting idea.
After #41635, there are max_rows and max_cols mechanics which are expected to be added as keywords to: Styler.to_html(). Currently this is only equivalent to a head scenario. The parsing and rendering mechanics were quite complicated so the equivalent of a tail might be df[::-1].style.to_html(max_rows=100) for example.

Do comment on the PR or further if you have any good ideas.

I guess there could be a parameter passed to methods such as bar, or a new method to be appended to the end of a call.

Nope don't want to add kwargs where they are not relevant to the function, but more add kwargs to rendering methods, or add to styler.options namespace.

@attack68 attack68 added Styler conditional formatting using DataFrame.style IO HTML read_html, to_html, Styler.apply, Styler.applymap and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 14, 2021
@dennymarcels
Copy link
Author

dennymarcels commented Jul 14, 2021

To be more precise, I like it when I call the DataFrame as the last line of code in a Colab cell and only the first 5 and last 5 rows are rendered. I was thinking something along that idea.

It would be nice to be able to call df.stile.bar().head(), or df.stile.bar().tail(), or even df.stile.bar().headAndTail().

@attack68
Copy link
Contributor

attack68 commented Jan 3, 2022

@dennymarcels please comment on #45176

@ivasve
Copy link

ivasve commented Jul 23, 2022

Hi @dennymarcels , did you have a look at issue #45176 if it has solved the issue you've been having?

@LinuxIsCool
Copy link

It's frustrating that max_rows can not be applied to a stylized dataframe.

@LinuxIsCool
Copy link

To deal with this for now, I just implemented this utility function in my code:

    def _truncate_dataframe(df, max_rows):
        return pd.concat([df.head(max_rows//2), df.tail(max_rows//2)])

@attack68
Copy link
Contributor

The "stylized dataframe" discussed in this thread is actually a Styler class. Your utility function involves DataFrames and does not reference a Styler. For Styler have you considered the global pandas option styler.render.max_rows ? https://pandas.pydata.org/docs/user_guide/options.html#available-options

@LinuxIsCool
Copy link

Thanks @attack68 for the fast response. That did indeed help me, but there is still a nuance issue of the aesthetics of the truncation. I would like to see the head and tail of the dataframe rather than just the head as was pointed out by @dennymarcels in this thread.

Here is my example:

import pandas as pd
import panel as pn
pd.set_option('styler.render.max_rows', 5)

df = pd.DataFrame(list(range(20)))

# Define CSS style for the sapphire color on column 'A' header
styles = [
    {'selector': 'th.col_heading.level0.col0', 
     'props': [('background-color', 'blue'), ('color', 'white')]}
]

# Apply the style to the DataFrame
styled_df = df.style.set_table_styles(styles, overwrite=False)

styled_df

image

@attack68
Copy link
Contributor

styled_df.hide(df.index[5:-5])

@attack68
Copy link
Contributor

I don't think the API for Styler is going to be expanded to for the head and tail case, nor with a show method. I think the solution to use hide whilst initially unintuitive is actually the best way to utilise all of Styler chained procedures.

Will close this issue and relabel the title for anyone searching for similar.

@attack68 attack68 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 28, 2024
@attack68 attack68 changed the title ENH: Enhance printing options for stylized DataFrames ENH: Enhance printing options for Styler: head and tail ? Apr 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement IO HTML read_html, to_html, Styler.apply, Styler.applymap Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants