git / code.ach.gov.ru / gavrin / jupyter_snippet
commit f22db1721a9fc7a8a6bcbaa60887d95c67c43b2a
author Дмитрий Сергеевич Гаврин <gavrin_ds@ach.gov.ru>
date 2020-03-29 15:53:56 +0300
parents root commit
message
пример поколоночной печати заголовка датафрейма
files
| file | add | del |
|---|---|---|
| dataframe_col_print.ipynb | +258 | -0 |
patch
diff --git a/dataframe_col_print.ipynb b/dataframe_col_print.ipynb
new file mode 100644
index 0000000000000000000000000000000000000000..2cc40a9dddf1dd52e59ad513b6e7bf5027c93b92
--- /dev/null
+++ b/dataframe_col_print.ipynb
@@ -0,0 +1,258 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Пример разбиения заголовка датафрейма и его печати"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import numpy as np\n",
+ "import pandas as pd"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "создаём простой датафрейм, с заголовками как в примере с ОКТМО"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "<div>\n",
+ "<style scoped>\n",
+ " .dataframe tbody tr th:only-of-type {\n",
+ " vertical-align: middle;\n",
+ " }\n",
+ "\n",
+ " .dataframe tbody tr th {\n",
+ " vertical-align: top;\n",
+ " }\n",
+ "\n",
+ " .dataframe thead th {\n",
+ " text-align: right;\n",
+ " }\n",
+ "</style>\n",
+ "<table border=\"1\" class=\"dataframe\">\n",
+ " <thead>\n",
+ " <tr style=\"text-align: right;\">\n",
+ " <th></th>\n",
+ " <th>guid</th>\n",
+ " <th>startdate</th>\n",
+ " <th>enddate</th>\n",
+ " <th>status</th>\n",
+ " <th>regioncode</th>\n",
+ " <th>areacode</th>\n",
+ " <th>citycode</th>\n",
+ " <th>localcode</th>\n",
+ " <th>controlnum</th>\n",
+ " <th>code</th>\n",
+ " <th>name</th>\n",
+ " <th>centrename</th>\n",
+ " <th>clarification</th>\n",
+ " <th>lastchangetype</th>\n",
+ " <th>changedate</th>\n",
+ " <th>introductiondate</th>\n",
+ " <th>section</th>\n",
+ " <th>lastChangeNum</th>\n",
+ " <th>filedate</th>\n",
+ " <th>loaddate</th>\n",
+ " </tr>\n",
+ " </thead>\n",
+ " <tbody>\n",
+ " </tbody>\n",
+ "</table>\n",
+ "</div>"
+ ],
+ "text/plain": [
+ "Empty DataFrame\n",
+ "Columns: [guid, startdate, enddate, status, regioncode, areacode, citycode, localcode, controlnum, code, name, centrename, clarification, lastchangetype, changedate, introductiondate, section, lastChangeNum, filedate, loaddate]\n",
+ "Index: []"
+ ]
+ },
+ "execution_count": 2,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "df = pd.DataFrame(\n",
+ " {'guid': [], 'startdate': [], 'enddate': [], 'status': [], 'regioncode': [], 'areacode': [], 'citycode': [],\n",
+ " 'localcode': [], 'controlnum': [], 'code': [], 'name': [], 'centrename': [], 'clarification': [],\n",
+ " 'lastchangetype': [], 'changedate': [], 'introductiondate': [], 'section': [], 'lastChangeNum': [], 'filedate': [], 'loaddate': []\n",
+ " }\n",
+ ")\n",
+ "df"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def print_df_cols(df, print_cols=2, tab_size=0):\n",
+ " '''\n",
+ " функция поколоночной печати заголовка dataframe\n",
+ " df - входной datafram\n",
+ " print_cols - во сколько колонок уложить печать\n",
+ " tab_size - задавать дополнительные отступы между колонками\n",
+ " '''\n",
+ " dfl = df.columns.size\n",
+ " full_col_list = list(df.columns) + [''] * (dfl * 2 % print_cols)\n",
+ " n = np.hstack(np.vsplit(np.array([np.arange(len(full_col_list)), full_col_list]).T, print_cols))\n",
+ " col_maxlen = [max(len(s) for s in n[:,i]) for i in range(1, print_cols * 2, 2)]\n",
+ " format_string = ' '.join('{:>3} {:%s}' % (m + tab_size) if m else '' for m in col_maxlen)\n",
+ " for col in n:\n",
+ " print(format_string.format(*col))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "напечатаем заголовки в 3 колонки"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ " 0 guid 7 localcode 14 changedate \n",
+ " 1 startdate 8 controlnum 15 introductiondate\n",
+ " 2 enddate 9 code 16 section \n",
+ " 3 status 10 name 17 lastChangeNum \n",
+ " 4 regioncode 11 centrename 18 filedate \n",
+ " 5 areacode 12 clarification 19 loaddate \n",
+ " 6 citycode 13 lastchangetype 20 \n"
+ ]
+ }
+ ],
+ "source": [
+ "print_df_cols(df, 3)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "а теперь с отступами"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ " 0 guid 7 localcode 14 changedate \n",
+ " 1 startdate 8 controlnum 15 introductiondate \n",
+ " 2 enddate 9 code 16 section \n",
+ " 3 status 10 name 17 lastChangeNum \n",
+ " 4 regioncode 11 centrename 18 filedate \n",
+ " 5 areacode 12 clarification 19 loaddate \n",
+ " 6 citycode 13 lastchangetype 20 \n"
+ ]
+ }
+ ],
+ "source": [
+ "print_df_cols(df, 3, 10)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "ну и в 2 колонки"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ " 0 guid 10 name \n",
+ " 1 startdate 11 centrename \n",
+ " 2 enddate 12 clarification \n",
+ " 3 status 13 lastchangetype \n",
+ " 4 regioncode 14 changedate \n",
+ " 5 areacode 15 introductiondate \n",
+ " 6 citycode 16 section \n",
+ " 7 localcode 17 lastChangeNum \n",
+ " 8 controlnum 18 filedate \n",
+ " 9 code 19 loaddate \n"
+ ]
+ }
+ ],
+ "source": [
+ "print_df_cols(df, 2, 10)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([], dtype=int32)"
+ ]
+ },
+ "execution_count": 17,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "np.arange(2, 2)"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.8.2"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 4
+}