5

I use Rust Polars and found it's kind of hard to working with it since not quite sure how to show all the columns.

use polars::df;

// use macro
let df = df! [
    "Column A 12345678910" => ["a", "b", "c"],
    "Column B 12345678910" => [1, 2, 3],
    "Column C 12345678910" => [Some(1), None, Some(3)],
    "Column D 12345678910" => [Some(1), None, Some(3)],
    "Column E 12345678910" => [Some(1), None, Some(3)],
    "Column F 12345678910" => [1, 2, 3],
    "Column G 12345678910" => [1, 2, 3],
    "Column Z 12345678910" => [1, 2, 3],
    "Column Ex 12345678910" => [Some(1), None, Some(3)],
    "Column Fs 12345678910" => [1, 2, 3],
    "Column Ga 12345678910" => [1, 2, 3],
    "Column Zz 12345678910" => [1, 2, 3],
]?;
println!("{:?}", df);

Output

shape: (3, 12)
+----------------------+----------------------+----------------------+----------------------+-----+-----------------------+-----------------------+-----------------------+-----------------------+
| Column A 12345678910 | Column B 12345678910 | Column C 12345678910 | Column D 12345678910 | ... | Column Ex 12345678910 | Column Fs 12345678910 | Column Ga 12345678910 | Column Zz 12345678910 |
| ---                  | ---                  | ---                  | ---                  |     | ---                   | ---                   | ---                   | ---                   |
| str                  | i32                  | i32                  | i32                  |     | i32                   | i32                   | i32                   | i32                   |
+======================+======================+======================+======================+=====+=======================+=======================+=======================+=======================+
| a                    | 1                    | 1                    | 1                    | ... | 1                     | 1                     | 1                     | 1                     |
+----------------------+----------------------+----------------------+----------------------+-----+-----------------------+-----------------------+-----------------------+-----------------------+
| b                    | 2                    | null                 | null                 | ... | null                  | 2                     | 2                     | 2                     |
+----------------------+----------------------+----------------------+----------------------+-----+-----------------------+-----------------------+-----------------------+-----------------------+
| c                    | 3                    | 3                    | 3                    | ... | 3                     | 3                     | 3                     | 3                     |
+----------------------+----------------------+----------------------+----------------------+-----+-----------------------+-----------------------+-----------------------+-----------------------+

Is it possible to show all the columns with Rust Polars?

3 Answers 3

5

You can change the defaults by setting POLARS_FMT_MAX_COLS to the number of columns you want as a maximum.

See more environment variable settings in the docs.

Sign up to request clarification or add additional context in comments.

Comments

5

I particularly like using rounded corners.
For example, I use this config env:

use polars::prelude::*;
use std::{
    env,
    error::Error,
};

fn main() -> Result<(), Box<dyn Error>> {
    // set MAX_COLS to 20
    configure_the_environment();

    // df with 12 columns
    let dataframe = df! [
        "Column A 12345678910" => ["a", "b", "c"],
        "Column B 12345678910" => [1, 2, 3],
        "Column C 12345678910" => [Some(1), None, Some(3)],
        "Column D 12345678910" => [Some(1), None, Some(3)],
        "Column E 12345678910" => [Some(1), None, Some(3)],
        "Column F 12345678910" => [1, 2, 3],
        "Column G 12345678910" => [1, 2, 3],
        "Column Z 12345678910" => [1, 2, 3],
        "Column Ex 12345678910" => [Some(1), None, Some(3)],
        "Column Fs 12345678910" => [1, 2, 3],
        "Column Ga 12345678910" => [1, 2, 3],
        "Column Zz 12345678910" => [1, 2, 3],
    ]?;

    println!("{:?}", dataframe);

    Ok(())
}

/// Configure Polars with ENV vars
pub fn configure_the_environment() {
    env::set_var("POLARS_FMT_TABLE_ROUNDED_CORNERS", "1"); // apply rounded corners to UTF8-styled tables.
    env::set_var("POLARS_FMT_MAX_COLS", "20"); // maximum number of columns shown when formatting DataFrames.
    env::set_var("POLARS_FMT_MAX_ROWS", "10"); // maximum number of rows shown when formatting DataFrames.
    env::set_var("POLARS_FMT_STR_LEN", "50");  // maximum number of characters printed per string value.
}

Comments

1

I wrote a helper function for my project that addresses this, that way I don't make global modifications if you only want to print the entire DataFrame in certain places in the code:

use polars::prelude::DataFrame;
use std::env;

pub fn print_full_df(df: &DataFrame, msg: Option<&str>) {
    // Save old settings
    let old_max_rows = env::var("POLARS_FMT_MAX_ROWS").ok();
    let old_max_cols = env::var("POLARS_FMT_MAX_COLS").ok();

    // Show everything
    env::set_var("POLARS_FMT_MAX_ROWS", "-1");
    env::set_var("POLARS_FMT_MAX_COLS", "-1");

    // Optional message, then DataFrame
    if let Some(m) = msg {
        println!("{}", m);
    }
    println!("{}", df);

    // Restore
    if let Some(val) = old_max_rows {
        env::set_var("POLARS_FMT_MAX_ROWS", val);
    } else {
        env::remove_var("POLARS_FMT_MAX_ROWS");
    }
    if let Some(val) = old_max_cols {
        env::set_var("POLARS_FMT_MAX_COLS", val);
    } else {
        env::remove_var("POLARS_FMT_MAX_COLS");
    }
}

You can use it like so:

// without message
print_full_df(&df, None);

// with a heading
print_full_df(&df, Some("Full DataFrame contents:"));

Optionally, you can have it truncate to the first x entries in the DF to avoid blowing up your logs with huge entries, if you are more interested in a subset of the data than the whole thing:

use polars::prelude::DataFrame;
use std::env;

pub fn print_full_df(df: &DataFrame, msg: Option<&str>) {
    // Save old POLARS_FMT settings
    let old_max_rows = env::var("POLARS_FMT_MAX_ROWS").ok();
    let old_max_cols = env::var("POLARS_FMT_MAX_COLS").ok();

    // Ensure we can see all columns (rows unlimited but we'll only print head)
    env::set_var("POLARS_FMT_MAX_ROWS", "-1");
    env::set_var("POLARS_FMT_MAX_COLS", "-1");

    // Optional message
    if let Some(m) = msg {
        println!("{}", m);
    }

    // Print only the first 5 rows (head will truncate to fewer if DF.len() < 5)
    let head_df = df.head(Some(5));
    println!("{}", head_df);

    // Restore previous settings
    if let Some(val) = old_max_rows {
        env::set_var("POLARS_FMT_MAX_ROWS", val);
    } else {
        env::remove_var("POLARS_FMT_MAX_ROWS");
    }
    if let Some(val) = old_max_cols {
        env::set_var("POLARS_FMT_MAX_COLS", val);
    } else {
        env::remove_var("POLARS_FMT_MAX_COLS");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.