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

Iterate over multiple columns for common formatting (GT_0.13.0, Polars_1.7.1) #503

Open
2 tasks done
starzar opened this issue Nov 2, 2024 · 2 comments
Open
2 tasks done

Comments

@starzar
Copy link

starzar commented Nov 2, 2024

Prework

Question

  1. How to iterate over multiple columns for common formatting on cells
    eg. converting all float values to int's, showing positive/negative numbers with green/red colors (Polars)?

fmt example for iterating over multiple columns does not work in this case .

  1. Also GT.show() import raises an import error .How to resolve this?

ERROR:

  loc.body(lambda x: x > 0 , columns= df_columns)  # fmt example https://posit-dev.github.io/great-tables/reference/GT.fmt.html#examples
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: LocBody.__init__() got multiple values for argument 'columns'

CODE:


import polars as pl
from great_tables import GT,loc,style

df = pl.DataFrame({
    "A": [10, -5, 20, -15],
    "B": [30.32234, 25.234234, -10.234234, 15.23423],
})

df_columns = df.columns

# Not supported on polars df
df_gt = GT(df)


filtered_df_gt = (
    df_gt
    .tab_style(
        style.fill("green"),
        loc.body(lambda x: x > 0 , columns= df_columns)  # fmt example https://posit-dev.github.io/great-tables/reference/GT.fmt.html#examples
    )
)
# ImportError: cannot import name 'show' from 'great_tables'
# df.show()


html_output = filtered_df_gt.as_raw_html()

html_path = "secondTerminal.html"
with open(html_path, "w") as file:
    file.write(html_output)
@jrycw
Copy link
Collaborator

jrycw commented Nov 3, 2024

@starzar , with Polars DataFrames, it’s often best to use Polars expressions for operations. There’s an example showing how to do this with a single column, but for multiple columns, we may need to loop through each to perform the desired operations. For instance:

import polars as pl
from great_tables import GT, loc, style

df = pl.DataFrame(
    {
        "A": [10, -5, 20, -15],
        "B": [30.32234, 25.234234, -10.234234, 15.23423],
    }
)


df_gt = GT(df)

for col in df.columns:
    df_gt = df_gt.tab_style(
        style=style.fill(
            (pl.when(pl.col(col) > 0).then(pl.lit("green")).otherwise(pl.lit("red")))
        ),
        locations=loc.body(columns=col),
    )

df_gt.show()

html_output = df_gt.as_raw_html()

html_path = "secondTerminal.html"
with open(html_path, "w") as file:
    file.write(html_output)

Image

For required packages, if you’re using Jupyter Notebook/Lab, be sure to install great_tables[extra], ipykernel, and polars.

@machow
Copy link
Collaborator

machow commented Nov 11, 2024

Thanks for the polars example @jrycw !

@starzar RE the .show() method, can you post what import great_tables; great_tables.__version__ gives? And the full import error stacktrace, if you can produce? I'm trying to figure out what might cause that..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants