{ "cells": [ { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "grade_id": "cell-e43ee4f9ec42e73b", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "# Working with data files\n", "\n", "```{admonition} Interactive page\n", ":class: warning, dropdown\n", "This is an interactive book page. Press launch button at the top right side.\n", "```\n", "\n", "Until now, we have seen how to generate data inside Python, e.g., by assigning values to arrays or using functions like `np.zeros()` and `np.linspace()`. However, what if we want to use Python to analyze **data from an experiment**? How do we get the data into Python? \n", "\n", "Sometimes, we might have just a **small number** of data points that we have measured by hand and wrote down on a piece of paper: say a list of measured optical densities OD600 of our bacterial culture (OD600 correlates with the number of cells). In this case, you can just \"load\" the data by defining Python arrays that contain your measured values:\n", "\n", "```\n", "# Time in hours\n", "t = np.array([0, 1, 2, 3, 4, 5, 6])\n", "\n", "# Measured OD600\n", "od = np.array([0.05, 0.10, 0.25, 0.50, 0.90, 1.50, 2.00]) \n", "```\n", "\n", "In a machine-controlled experiment, though, you may have a data file with hundreds, thousands, or even millions of data points, for example, voltage measured as a function of time using an oscilloscope, or omics data (genomics, proteomics) in bioinformatics. \n", "To perform operations, analyze, and plot **large data**, you can always resort to Python. Using a programming language such as Python has multiple **advantages** over MS Excel:\n", " - **Scalability**: Python can handle large data much more efficiently and has no limit on the number of rows and columns.\n", " - **Automation**: You can write a script to automate a repetitive task, which is useful for data cleaning, transformation and repetitive calculations, as well as when you need to handle multiple data files in one go (imagine if you ran a transcriptomics experiment on 60 samples).\n", " - **Data analysis and manipulation:** Python has libraries that allow you to manipulate and visualize data in a more powerful way and with more options than you'd get in Excel.\n", "\n", "In this chapter, we will explore how to load, manipulate, and save data files in Python. For this purpose, we will use specific Python packages, namely NumPy and pandas." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## NumPy vs. pandas\n", "\n", "We've already become familiar with the NumPy library for scientific computing. Here we introduce another Python data analysis library - **pandas**.\n", "\n", "Both NumPy and pandas are fundamental Python libraries that can be used for **data manipulation and analysis**. While they have some overlapping functionalities, they were designed for different purposes and are often used side by side. \n", "\n", "**NumPy** is meant for (efficient) numerical computations, provides support for $N$-dimensional NumPy arrays, and has a collection of mathematical functions that can be applied to arrays (you can think of linear algebra and statistics). Operations on NumPy arrays are more efficient than operations on lists. Therefore, we use NumPy for:\n", "- Numerical computations, scientific computing\n", "- Handling multidimensional data\n", "- Mathematical and statistical operations on large datasets\n", "\n", "**pandas** is built on top of NumPy and is aimed specifically at data manipulation and analysis. Just like NumPy has its special data type called *NumPy arrays*, pandas offers high-level data structures:\n", "- *Series*: 1D labeled array that can contain any data type\n", "- *DataFrame*: 2D labeled data structure, where columns can have data of different types \n", "\n", "pandas is great for data manipulation (e.g., merging and joining datasets) and analysis. With pandas, you can read and write files of various formats, such as CSV and Excel.\n", "\n", "```{figure} ../images/chapter9/numpy-vs-pandas.png\n", "---\n", "height: 120px\n", "name: numpy-vs-pandas\n", "---\n", "NumPy vs. pandas library for working with files. (NumPy logo is licensed under CC BY-SA.)\n", "```\n", "\n", "**When should we use each of these libraries?**\n", "\n", "* Speed:\n", " * NumPy is more efficient for large numerical computations due to its optimized array operations.\n", " * pandas is less efficient due to its additional functionalities but is optimized for high-level data manipulation.\n", "* Data structures:\n", " * NumPy is suitable for homogeneous data (all elements of the same type).\n", " * pandas is suitable for heterogeneous data (different types within the same data structure).\n", "* Functionality:\n", " * NumPy is focused on numerical computations, less on data manipulation.\n", " * pandas has extensive data manipulation capabilities, ideal for cleaning, transforming, and analyzing structured data.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "thebe-remove-input-init" ] }, "outputs": [], "source": [ "import micropip\n", "await micropip.install(\"jupyterquiz\")\n", "from jupyterquiz import display_quiz\n", "import json\n", "\n", "with open(\"questions4.json\", \"r\") as file:\n", " questions=json.load(file)\n", " \n", "display_quiz(questions, border_radius=0)" ] } ], "metadata": { "celltoolbar": "Create Assignment", "jupytext": { "formats": "ipynb,md" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.4" }, "toc": { "base_numbering": "5", "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": { "height": "calc(100% - 180px)", "left": "10px", "top": "150px", "width": "249.797px" }, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 4 }