Skip to content

Commit 08cf8db

Browse files
committed
ENH 14194: add style option for hiding index and columns
1 parent 6da85b3 commit 08cf8db

File tree

4 files changed

+275
-29
lines changed

4 files changed

+275
-29
lines changed

doc/source/style.ipynb

+69-12
Original file line numberDiff line numberDiff line change
@@ -674,13 +674,14 @@
674674
"- precision\n",
675675
"- captions\n",
676676
"- table-wide styles\n",
677+
"- hiding the index or columns\n",
677678
"\n",
678679
"Each of these can be specified in two ways:\n",
679680
"\n",
680681
"- A keyword argument to `Styler.__init__`\n",
681-
"- A call to one of the `.set_` methods, e.g. `.set_caption`\n",
682+
"- A call to one of the `.set_` or `.hide_` methods, e.g. `.set_caption` or `.hide_columns`\n",
682683
"\n",
683-
"The best method to use depends on the context. Use the `Styler` constructor when building many styled DataFrames that should all share the same properties. For interactive use, the`.set_` methods are more convenient."
684+
"The best method to use depends on the context. Use the `Styler` constructor when building many styled DataFrames that should all share the same properties. For interactive use, the`.set_` and `.hide_` methods are more convenient."
684685
]
685686
},
686687
{
@@ -814,6 +815,38 @@
814815
"We hope to collect some useful ones either in pandas, or preferable in a new package that [builds on top](#Extensibility) the tools here."
815816
]
816817
},
818+
{
819+
"cell_type": "markdown",
820+
"metadata": {},
821+
"source": [
822+
"### Hiding the Index or Columns"
823+
]
824+
},
825+
{
826+
"cell_type": "markdown",
827+
"metadata": {},
828+
"source": [
829+
"The index can be hidden from rendering by calling `Styler.hide_index`. Columns can be hidden from rendering by calling `Styler.hide_columns` and passing in the name of a column, or a slice of columns."
830+
]
831+
},
832+
{
833+
"cell_type": "code",
834+
"execution_count": null,
835+
"metadata": {},
836+
"outputs": [],
837+
"source": [
838+
"df.style.hide_index()"
839+
]
840+
},
841+
{
842+
"cell_type": "code",
843+
"execution_count": null,
844+
"metadata": {},
845+
"outputs": [],
846+
"source": [
847+
"df.style.hide_columns(['C','D'])"
848+
]
849+
},
817850
{
818851
"cell_type": "markdown",
819852
"metadata": {},
@@ -875,7 +908,9 @@
875908
{
876909
"cell_type": "code",
877910
"execution_count": null,
878-
"metadata": {},
911+
"metadata": {
912+
"collapsed": true
913+
},
879914
"outputs": [],
880915
"source": [
881916
"from IPython.html import widgets\n",
@@ -911,7 +946,9 @@
911946
{
912947
"cell_type": "code",
913948
"execution_count": null,
914-
"metadata": {},
949+
"metadata": {
950+
"collapsed": true
951+
},
915952
"outputs": [],
916953
"source": [
917954
"np.random.seed(25)\n",
@@ -1010,7 +1047,9 @@
10101047
{
10111048
"cell_type": "code",
10121049
"execution_count": null,
1013-
"metadata": {},
1050+
"metadata": {
1051+
"collapsed": true
1052+
},
10141053
"outputs": [],
10151054
"source": [
10161055
"%mkdir templates"
@@ -1027,7 +1066,9 @@
10271066
{
10281067
"cell_type": "code",
10291068
"execution_count": null,
1030-
"metadata": {},
1069+
"metadata": {
1070+
"collapsed": true
1071+
},
10311072
"outputs": [],
10321073
"source": [
10331074
"%%file templates/myhtml.tpl\n",
@@ -1078,7 +1119,9 @@
10781119
{
10791120
"cell_type": "code",
10801121
"execution_count": null,
1081-
"metadata": {},
1122+
"metadata": {
1123+
"collapsed": true
1124+
},
10821125
"outputs": [],
10831126
"source": [
10841127
"MyStyler(df)"
@@ -1094,7 +1137,9 @@
10941137
{
10951138
"cell_type": "code",
10961139
"execution_count": null,
1097-
"metadata": {},
1140+
"metadata": {
1141+
"collapsed": true
1142+
},
10981143
"outputs": [],
10991144
"source": [
11001145
"HTML(MyStyler(df).render(table_title=\"Extending Example\"))"
@@ -1110,7 +1155,9 @@
11101155
{
11111156
"cell_type": "code",
11121157
"execution_count": null,
1113-
"metadata": {},
1158+
"metadata": {
1159+
"collapsed": true
1160+
},
11141161
"outputs": [],
11151162
"source": [
11161163
"EasyStyler = Styler.from_custom_template(\"templates\", \"myhtml.tpl\")\n",
@@ -1127,7 +1174,9 @@
11271174
{
11281175
"cell_type": "code",
11291176
"execution_count": null,
1130-
"metadata": {},
1177+
"metadata": {
1178+
"collapsed": true
1179+
},
11311180
"outputs": [],
11321181
"source": [
11331182
"with open(\"template_structure.html\") as f:\n",
@@ -1147,6 +1196,7 @@
11471196
"cell_type": "code",
11481197
"execution_count": null,
11491198
"metadata": {
1199+
"collapsed": true,
11501200
"nbsphinx": "hidden"
11511201
},
11521202
"outputs": [],
@@ -1163,7 +1213,7 @@
11631213
],
11641214
"metadata": {
11651215
"kernelspec": {
1166-
"display_name": "Python 3",
1216+
"display_name": "Python [default]",
11671217
"language": "python",
11681218
"name": "python3"
11691219
},
@@ -1177,7 +1227,14 @@
11771227
"name": "python",
11781228
"nbconvert_exporter": "python",
11791229
"pygments_lexer": "ipython3",
1180-
"version": "3.6.1"
1230+
"version": "3.5.3"
1231+
},
1232+
"widgets": {
1233+
"application/vnd.jupyter.widget-state+json": {
1234+
"state": {},
1235+
"version_major": 1,
1236+
"version_minor": 0
1237+
}
11811238
}
11821239
},
11831240
"nbformat": 4,

doc/source/whatsnew/v0.21.0.txt

+3
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ Other Enhancements
163163
- :func:`Categorical.rename_categories` now accepts a dict-like argument as `new_categories` and only updates the categories found in that dict. (:issue:`17336`)
164164
- :func:`read_excel` raises ``ImportError`` with a better message if ``xlrd`` is not installed. (:issue:`17613`)
165165
- :meth:`DataFrame.assign` will preserve the original order of ``**kwargs`` for Python 3.6+ users instead of sorting the column names
166+
- :class:`pandas.io.formats.style.Styler` now has ``index`` parameter and corresponding method ``hide_index()`` to determine whether the index will be rendered in ouptut (:issue:`14194`)
167+
- :class:`pandas.io.formats.style.Styler` now has ``hidden_cols`` parameter and corresponding method ``hide_columns()`` to determine whether columns will be hidden in output (:issue:`14194`)
168+
166169

167170

168171
.. _whatsnew_0210.api_breaking:

0 commit comments

Comments
 (0)