Skip to content

Instantly share code, notes, and snippets.

@lisphilar
Created April 21, 2024 15:27
Show Gist options
  • Save lisphilar/6036542413e2b5991751d706541a7795 to your computer and use it in GitHub Desktop.
Save lisphilar/6036542413e2b5991751d706541a7795 to your computer and use it in GitHub Desktop.
polars_0-20-22_cell_alignment_display.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"toc_visible": true,
"authorship_tag": "ABX9TyPcSVk1Grl9ACJS1/z8/zp0",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/lisphilar/6036542413e2b5991751d706541a7795/polars_0-20-22_cell_alignment_display.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"!pip install polars -U"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "5aJNaFi5_ZIO",
"outputId": "d0675aa6-af39-4037-8fa3-f1a5a8bd097b"
},
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Requirement already satisfied: polars in /usr/local/lib/python3.10/dist-packages (0.20.2)\n",
"Collecting polars\n",
" Downloading polars-0.20.22-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (27.4 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m27.4/27.4 MB\u001b[0m \u001b[31m13.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hInstalling collected packages: polars\n",
" Attempting uninstall: polars\n",
" Found existing installation: polars 0.20.2\n",
" Uninstalling polars-0.20.2:\n",
" Successfully uninstalled polars-0.20.2\n",
"Successfully installed polars-0.20.22\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"from datetime import date\n",
"import polars as pl"
],
"metadata": {
"id": "w78pfSbW_ZIO"
},
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"source": [
"df = pl.DataFrame(\n",
" {\n",
" \"long_word\": [\"Hollo Word!\", \"Thank you!\", \"Blazingly fast DataFrame!!\"],\n",
" \"variable\": [\"group_id\", \"group_code\", \"group_name\"],\n",
" \"column_abc\": [1.0, 2.5, 5.0],\n",
" \"column_xyz\": [True, False, True],\n",
" \"abc\": [11, 2, 333],\n",
" \"mno\": [date(2023, 10, 29), None, date(2001, 7, 5)],\n",
" \"xyz\": [True, False, None],\n",
" }\n",
")"
],
"metadata": {
"id": "euRA_zzD_ZIO"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"source": [
"pl.Config.set_tbl_cell_alignment(\"LEFT\")\n",
"pl.Config.set_tbl_cell_numeric_alignment(\"LEFT\")\n",
"_ = pl.Config.set_fmt_str_lengths(50)"
],
"metadata": {
"id": "r0edWr4Q_ZIO"
},
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"source": [
"print(df)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "e4918e27-734b-425a-9fba-5d5f9d72a0fd",
"id": "VKZULLNY_ZIO"
},
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"shape: (3, 7)\n",
"┌────────────────────────────┬────────────┬────────────┬────────────┬─────┬────────────┬───────┐\n",
"│ long_word ┆ variable ┆ column_abc ┆ column_xyz ┆ abc ┆ mno ┆ xyz │\n",
"│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n",
"│ str ┆ str ┆ f64 ┆ bool ┆ i64 ┆ date ┆ bool │\n",
"╞════════════════════════════╪════════════╪════════════╪════════════╪═════╪════════════╪═══════╡\n",
"│ Hollo Word! ┆ group_id ┆ 1.0 ┆ true ┆ 11 ┆ 2023-10-29 ┆ true │\n",
"│ Thank you! ┆ group_code ┆ 2.5 ┆ false ┆ 2 ┆ null ┆ false │\n",
"│ Blazingly fast DataFrame!! ┆ group_name ┆ 5.0 ┆ true ┆ 333 ┆ 2001-07-05 ┆ null │\n",
"└────────────────────────────┴────────────┴────────────┴────────────┴─────┴────────────┴───────┘\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"display(df)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 192
},
"outputId": "523a44d8-7ed5-4d72-c404-7619612c339d",
"id": "gKXaf4VV_ZIO"
},
"execution_count": 6,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"shape: (3, 7)\n",
"┌────────────────────────────┬────────────┬────────────┬────────────┬─────┬────────────┬───────┐\n",
"│ long_word ┆ variable ┆ column_abc ┆ column_xyz ┆ abc ┆ mno ┆ xyz │\n",
"│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n",
"│ str ┆ str ┆ f64 ┆ bool ┆ i64 ┆ date ┆ bool │\n",
"╞════════════════════════════╪════════════╪════════════╪════════════╪═════╪════════════╪═══════╡\n",
"│ Hollo Word! ┆ group_id ┆ 1.0 ┆ true ┆ 11 ┆ 2023-10-29 ┆ true │\n",
"│ Thank you! ┆ group_code ┆ 2.5 ┆ false ┆ 2 ┆ null ┆ false │\n",
"│ Blazingly fast DataFrame!! ┆ group_name ┆ 5.0 ┆ true ┆ 333 ┆ 2001-07-05 ┆ null │\n",
"└────────────────────────────┴────────────┴────────────┴────────────┴─────┴────────────┴───────┘"
],
"text/html": [
"<div><style>\n",
".dataframe > thead > tr,\n",
".dataframe > tbody > tr {\n",
" text-align: right;\n",
" white-space: pre-wrap;\n",
"}\n",
"</style>\n",
"<small>shape: (3, 7)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>long_word</th><th>variable</th><th>column_abc</th><th>column_xyz</th><th>abc</th><th>mno</th><th>xyz</th></tr><tr><td>str</td><td>str</td><td>f64</td><td>bool</td><td>i64</td><td>date</td><td>bool</td></tr></thead><tbody><tr><td>&quot;Hollo Word!&quot;</td><td>&quot;group_id&quot;</td><td>1.0</td><td>true</td><td>11</td><td>2023-10-29</td><td>true</td></tr><tr><td>&quot;Thank you!&quot;</td><td>&quot;group_code&quot;</td><td>2.5</td><td>false</td><td>2</td><td>null</td><td>false</td></tr><tr><td>&quot;Blazingly fast DataFrame!!&quot;</td><td>&quot;group_name&quot;</td><td>5.0</td><td>true</td><td>333</td><td>2001-07-05</td><td>null</td></tr></tbody></table></div>"
]
},
"metadata": {}
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment