{ "cells": [ { "cell_type": "markdown", "id": "259f63f4", "metadata": {}, "source": [ "# Excel\n", "\n", "pandas also supports reading table data stored in Excel 2003 (and higher) files, either with the `ExcelFile` class or the `pandas.read_excel` function. Internally, these tools use the add-on packages [xlrd](https://xlrd.readthedocs.io/en/latest/) and [openpyxl](https://openpyxl.readthedocs.io/en/stable/) to read XLS and XLSX files respectively. These must be installed separately from pandas with uv.\n", "\n", "To use `ExcelFile`, create an instance by passing a path to an xls or xlsx file:" ] }, { "cell_type": "code", "execution_count": 1, "id": "f183dfbe", "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "id": "40329ed1", "metadata": {}, "outputs": [], "source": [ "xlsx = pd.ExcelFile(\"library.xlsx\")" ] }, { "cell_type": "markdown", "id": "5cf907fa", "metadata": {}, "source": [ "You can then display the sheets of the file with:" ] }, { "cell_type": "code", "execution_count": 3, "id": "2cc064ce", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['books']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xlsx.sheet_names" ] }, { "cell_type": "code", "execution_count": 4, "id": "96c75469", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TitelSpracheAutor*innenLizenzVeröffentlichungsdatum
0Python basicsenVeit SchieleBSD-3-Clause2021-10-28
1Jupyter TutorialenVeit SchieleBSD-3-Clause2019-06-27
2Jupyter TutorialdeVeit SchieleBSD-3-Clause2020-10-26
3PyViz TutorialenVeit SchieleBSD-3-Clause2020-04-13
\n", "
" ], "text/plain": [ " Titel Sprache Autor*innen Lizenz Veröffentlichungsdatum\n", "0 Python basics en Veit Schiele BSD-3-Clause 2021-10-28\n", "1 Jupyter Tutorial en Veit Schiele BSD-3-Clause 2019-06-27\n", "2 Jupyter Tutorial de Veit Schiele BSD-3-Clause 2020-10-26\n", "3 PyViz Tutorial en Veit Schiele BSD-3-Clause 2020-04-13" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "books = pd.read_excel(xlsx, \"books\")\n", "\n", "books" ] }, { "cell_type": "markdown", "id": "52a2a7a6", "metadata": {}, "source": [ "If you are reading in multiple sheets of a file, it is quicker to create the Excel file, but you can also just pass the file name to `pandas.read_excel`:" ] }, { "cell_type": "code", "execution_count": 5, "id": "47bcb108", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TitelSpracheAutor*innenLizenzVeröffentlichungsdatum
0Python basicsenVeit SchieleBSD-3-Clause2021-10-28
1Jupyter TutorialenVeit SchieleBSD-3-Clause2019-06-27
2Jupyter TutorialdeVeit SchieleBSD-3-Clause2020-10-26
3PyViz TutorialenVeit SchieleBSD-3-Clause2020-04-13
\n", "
" ], "text/plain": [ " Titel Sprache Autor*innen Lizenz Veröffentlichungsdatum\n", "0 Python basics en Veit Schiele BSD-3-Clause 2021-10-28\n", "1 Jupyter Tutorial en Veit Schiele BSD-3-Clause 2019-06-27\n", "2 Jupyter Tutorial de Veit Schiele BSD-3-Clause 2020-10-26\n", "3 PyViz Tutorial en Veit Schiele BSD-3-Clause 2020-04-13" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.read_excel(\"library.xlsx\", \"books\")" ] }, { "cell_type": "markdown", "id": "fcf9add3", "metadata": {}, "source": [ "To write pandas data in Excel format, you must first create an `ExcelWriter` and then write data to it using [pandas.DataFrame.to_excel](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_excel.html):" ] }, { "cell_type": "code", "execution_count": 6, "id": "f14bcd9f", "metadata": {}, "outputs": [], "source": [ "writer = pd.ExcelWriter(\"library.xlsx\")\n", "books.to_excel(writer, \"books\")\n", "writer.close()" ] }, { "cell_type": "markdown", "id": "8e28801c", "metadata": {}, "source": [ "You can also pass a file path `to_excel` and thus bypass the `ExcelWriter`:" ] }, { "cell_type": "code", "execution_count": 7, "id": "79d7b65a", "metadata": {}, "outputs": [], "source": [ "books.to_excel(\"library.xlsx\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.11 Kernel", "language": "python", "name": "python311" }, "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.11.10" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }