{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "source": [ "#### Copyright 2017 Google LLC." ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "JndnmDMp66FL" } }, { "source": [ "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "hMqWDc_m6rUC", "colab": { "autoexec": { "wait_interval": 0, "startup": false } }, "cellView": "both" }, "outputs": [], "execution_count": 0 }, { "source": [ " # Pandas \u7b80\u4ecb" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "rHLcriKWLRe4" } }, { "source": [ "**\u5b66\u4e60\u76ee\u6807\uff1a**\n", " * \u5927\u81f4\u4e86\u89e3 *pandas* \u5e93\u7684 `DataFrame` \u548c `Series` \u6570\u636e\u7ed3\u6784\n", " * \u5b58\u53d6\u548c\u5904\u7406 `DataFrame` \u548c `Series` \u4e2d\u7684\u6570\u636e\n", " * \u5c06 CSV \u6570\u636e\u5bfc\u5165 pandas \u5e93\u7684 `DataFrame`\n", " * \u5bf9 `DataFrame` \u91cd\u5efa\u7d22\u5f15\u6765\u968f\u673a\u6253\u4e71\u6570\u636e" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "QvJBqX8_Bctk" } }, { "source": [ " [*pandas*](http://pandas.pydata.org/) \u662f\u4e00\u79cd\u5217\u5b58\u6570\u636e\u5206\u6790 API\u3002\u5b83\u662f\u7528\u4e8e\u5904\u7406\u548c\u5206\u6790\u8f93\u5165\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u5f88\u591a\u673a\u5668\u5b66\u4e60\u6846\u67b6\u90fd\u652f\u6301\u5c06 *pandas* \u6570\u636e\u7ed3\u6784\u4f5c\u4e3a\u8f93\u5165\u3002\n", "\u867d\u7136\u5168\u65b9\u4f4d\u4ecb\u7ecd *pandas* API \u4f1a\u5360\u636e\u5f88\u957f\u7bc7\u5e45\uff0c\u4f46\u5b83\u7684\u6838\u5fc3\u6982\u5ff5\u975e\u5e38\u7b80\u5355\uff0c\u6211\u4eec\u4f1a\u5728\u4e0b\u6587\u4e2d\u8fdb\u884c\u8bf4\u660e\u3002\u6709\u5173\u66f4\u5b8c\u6574\u7684\u53c2\u8003\uff0c\u8bf7\u8bbf\u95ee [*pandas* \u6587\u6863\u7f51\u7ad9](http://pandas.pydata.org/pandas-docs/stable/index.html)\uff0c\u5176\u4e2d\u5305\u542b\u4e30\u5bcc\u7684\u6587\u6863\u548c\u6559\u7a0b\u8d44\u6e90\u3002" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "TIFJ83ZTBctl" } }, { "source": [ " ## \u57fa\u672c\u6982\u5ff5\n", "\n", "\u4ee5\u4e0b\u884c\u5bfc\u5165\u4e86 *pandas* API \u5e76\u8f93\u51fa\u4e86\u76f8\u5e94\u7684 API \u7248\u672c\uff1a" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "s_JOISVgmn9v" } }, { "source": [ "from __future__ import print_function\n\n", "import pandas as pd\n", "pd.__version__" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "aSRYu62xUi3g", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ " *pandas* \u4e2d\u7684\u4e3b\u8981\u6570\u636e\u7ed3\u6784\u88ab\u5b9e\u73b0\u4e3a\u4ee5\u4e0b\u4e24\u7c7b\uff1a\n", "\n", " * **`DataFrame`**\uff0c\u60a8\u53ef\u4ee5\u5c06\u5b83\u60f3\u8c61\u6210\u4e00\u4e2a\u5173\u7cfb\u578b\u6570\u636e\u8868\u683c\uff0c\u5176\u4e2d\u5305\u542b\u591a\u4e2a\u884c\u548c\u5df2\u547d\u540d\u7684\u5217\u3002\n", " * **`Series`**\uff0c\u5b83\u662f\u5355\u4e00\u5217\u3002`DataFrame` \u4e2d\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a `Series`\uff0c\u6bcf\u4e2a `Series` \u5747\u6709\u4e00\u4e2a\u540d\u79f0\u3002\n", "\n", "\u6570\u636e\u6846\u67b6\u662f\u7528\u4e8e\u6570\u636e\u64cd\u63a7\u7684\u4e00\u79cd\u5e38\u7528\u62bd\u8c61\u5b9e\u73b0\u5f62\u5f0f\u3002[Spark](https://spark.apache.org/) \u548c [R](https://www.r-project.org/about.html) \u4e2d\u4e5f\u6709\u7c7b\u4f3c\u7684\u5b9e\u73b0\u3002" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "daQreKXIUslr" } }, { "source": [ " \u521b\u5efa `Series` \u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u6784\u5efa `Series` \u5bf9\u8c61\u3002\u4f8b\u5982\uff1a" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "fjnAk1xcU0yc" } }, { "source": [ "pd.Series(['San Francisco', 'San Jose', 'Sacramento'])" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "DFZ42Uq7UFDj", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ " \u60a8\u53ef\u4ee5\u5c06\u6620\u5c04 `string` \u5217\u540d\u79f0\u7684 `dict` \u4f20\u9012\u5230\u5b83\u4eec\u5404\u81ea\u7684 `Series`\uff0c\u4ece\u800c\u521b\u5efa`DataFrame`\u5bf9\u8c61\u3002\u5982\u679c `Series` \u5728\u957f\u5ea6\u4e0a\u4e0d\u4e00\u81f4\uff0c\u7cfb\u7edf\u4f1a\u7528\u7279\u6b8a\u7684 [NA/NaN](http://pandas.pydata.org/pandas-docs/stable/missing_data.html) \u503c\u586b\u5145\u7f3a\u5931\u7684\u503c\u3002\u4f8b\u5982\uff1a" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "U5ouUp1cU6pC" } }, { "source": [ "city_names = pd.Series(['San Francisco', 'San Jose', 'Sacramento'])\n", "population = pd.Series([852469, 1015785, 485199])\n", "\n", "pd.DataFrame({ 'City name': city_names, 'Population': population })" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "avgr6GfiUh8t", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ " \u4f46\u662f\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u60a8\u9700\u8981\u5c06\u6574\u4e2a\u6587\u4ef6\u52a0\u8f7d\u5230 `DataFrame` \u4e2d\u3002\u4e0b\u9762\u7684\u793a\u4f8b\u52a0\u8f7d\u4e86\u4e00\u4e2a\u5305\u542b\u52a0\u5229\u798f\u5c3c\u4e9a\u5dde\u4f4f\u623f\u6570\u636e\u7684\u6587\u4ef6\u3002\u8bf7\u8fd0\u884c\u4ee5\u4e0b\u5355\u5143\u683c\u4ee5\u52a0\u8f7d\u6570\u636e\uff0c\u5e76\u521b\u5efa\u7279\u5f81\u5b9a\u4e49\uff1a" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "oa5wfZT7VHJl" } }, { "source": [ "california_housing_dataframe = pd.read_csv(\"https://download.mlcc.google.cn/mledu-datasets/california_housing_train.csv\", sep=\",\")\n", "california_housing_dataframe.describe()" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "av6RYOraVG1V", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ " \u4e0a\u9762\u7684\u793a\u4f8b\u4f7f\u7528 `DataFrame.describe` \u6765\u663e\u793a\u5173\u4e8e `DataFrame` \u7684\u6709\u8da3\u7edf\u8ba1\u4fe1\u606f\u3002\u53e6\u4e00\u4e2a\u5b9e\u7528\u51fd\u6570\u662f `DataFrame.head`\uff0c\u5b83\u663e\u793a `DataFrame` \u7684\u524d\u51e0\u4e2a\u8bb0\u5f55\uff1a" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "WrkBjfz5kEQu" } }, { "source": [ "california_housing_dataframe.head()" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "s3ND3bgOkB5k", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ " *pandas* \u7684\u53e6\u4e00\u4e2a\u5f3a\u5927\u529f\u80fd\u662f\u7ed8\u5236\u56fe\u8868\u3002\u4f8b\u5982\uff0c\u501f\u52a9 `DataFrame.hist`\uff0c\u60a8\u53ef\u4ee5\u5feb\u901f\u4e86\u89e3\u4e00\u4e2a\u5217\u4e2d\u503c\u7684\u5206\u5e03\uff1a" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "w9-Es5Y6laGd" } }, { "source": [ "california_housing_dataframe.hist('housing_median_age')" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "nqndFVXVlbPN", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ " ## \u8bbf\u95ee\u6570\u636e\n", "\n", "\u60a8\u53ef\u4ee5\u4f7f\u7528\u719f\u6089\u7684 Python dict/list \u6307\u4ee4\u8bbf\u95ee `DataFrame` \u6570\u636e\uff1a" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "XtYZ7114n3b-" } }, { "source": [ "cities = pd.DataFrame({ 'City name': city_names, 'Population': population })\n", "print(type(cities['City name']))\n", "cities['City name']" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "_TFm7-looBFF", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ "print(type(cities['City name'][1]))\n", "cities['City name'][1]" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "V5L6xacLoxyv", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ "print(type(cities[0:2]))\n", "cities[0:2]" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "gcYX1tBPugZl", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ " \u6b64\u5916\uff0c*pandas* \u9488\u5bf9\u9ad8\u7ea7[\u7d22\u5f15\u548c\u9009\u62e9](http://pandas.pydata.org/pandas-docs/stable/indexing.html)\u63d0\u4f9b\u4e86\u6781\u5176\u4e30\u5bcc\u7684 API\uff08\u6570\u91cf\u8fc7\u591a\uff0c\u6b64\u5904\u65e0\u6cd5\u9010\u4e00\u5217\u51fa\uff09\u3002" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "65g1ZdGVjXsQ" } }, { "source": [ " ## \u64cd\u63a7\u6570\u636e\n", "\n", "\u60a8\u53ef\u4ee5\u5411 `Series` \u5e94\u7528 Python \u7684\u57fa\u672c\u8fd0\u7b97\u6307\u4ee4\u3002\u4f8b\u5982\uff1a" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "RM1iaD-ka3Y1" } }, { "source": [ "population / 1000." ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "XWmyCFJ5bOv-", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ " [NumPy](http://www.numpy.org/) \u662f\u4e00\u79cd\u7528\u4e8e\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u7684\u5e38\u7528\u5de5\u5177\u5305\u3002*pandas* `Series` \u53ef\u7528\u4f5c\u5927\u591a\u6570 NumPy \u51fd\u6570\u7684\u53c2\u6570\uff1a" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "TQzIVnbnmWGM" } }, { "source": [ "import numpy as np\n", "\n", "np.log(population)" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "ko6pLK6JmkYP", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ " \u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u5355\u5217\u8f6c\u6362\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 `Series.apply`\u3002\u50cf Python [\u6620\u5c04\u51fd\u6570](https://docs.python.org/2/library/functions.html#map)\u4e00\u6837\uff0c`Series.apply` \u5c06\u4ee5\u53c2\u6570\u5f62\u5f0f\u63a5\u53d7 [lambda \u51fd\u6570](https://docs.python.org/2/tutorial/controlflow.html#lambda-expressions)\uff0c\u800c\u8be5\u51fd\u6570\u4f1a\u5e94\u7528\u4e8e\u6bcf\u4e2a\u503c\u3002\n", "\n", "\u4e0b\u9762\u7684\u793a\u4f8b\u521b\u5efa\u4e86\u4e00\u4e2a\u6307\u660e `population` \u662f\u5426\u8d85\u8fc7 100 \u4e07\u7684\u65b0 `Series`\uff1a" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "xmxFuQmurr6d" } }, { "source": [ "population.apply(lambda val: val > 1000000)" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "Fc1DvPAbstjI", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ " \n", "`DataFrames` \u7684\u4fee\u6539\u65b9\u5f0f\u4e5f\u975e\u5e38\u7b80\u5355\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5411\u73b0\u6709 `DataFrame` \u6dfb\u52a0\u4e86\u4e24\u4e2a `Series`\uff1a" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "ZeYYLoV9b9fB" } }, { "source": [ "cities['Area square miles'] = pd.Series([46.87, 176.53, 97.92])\n", "cities['Population density'] = cities['Population'] / cities['Area square miles']\n", "cities" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "0gCEX99Hb8LR", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ " ## \u7ec3\u4e60 1\n", "\n", "\u901a\u8fc7\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u5e03\u5c14\u503c\u5217\uff08\u5f53\u4e14\u4ec5\u5f53\u4ee5\u4e0b*\u4e24\u9879*\u5747\u4e3a True \u65f6\u4e3a True\uff09\u4fee\u6539 `cities` \u8868\u683c\uff1a\n", "\n", " * \u57ce\u5e02\u4ee5\u5723\u4eba\u547d\u540d\u3002\n", " * \u57ce\u5e02\u9762\u79ef\u5927\u4e8e 50 \u5e73\u65b9\u82f1\u91cc\u3002\n", "\n", "**\u6ce8\u610f\uff1a**\u5e03\u5c14\u503c `Series` \u662f\u4f7f\u7528\u201c\u6309\u4f4d\u201d\u800c\u975e\u4f20\u7edf\u5e03\u5c14\u503c\u201c\u8fd0\u7b97\u7b26\u201d\u7ec4\u5408\u7684\u3002\u4f8b\u5982\uff0c\u6267\u884c*\u903b\u8f91\u4e0e*\u65f6\uff0c\u5e94\u4f7f\u7528 `&`\uff0c\u800c\u4e0d\u662f `and`\u3002\n", "\n", "**\u63d0\u793a\uff1a**\"San\" \u5728\u897f\u73ed\u7259\u8bed\u4e2d\u610f\u4e3a \"saint\"\u3002" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "6qh63m-ayb-c" } }, { "source": [ "# Your code here" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "zCOn8ftSyddH", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ " ### \u89e3\u51b3\u65b9\u6848\n", "\n", "\u70b9\u51fb\u4e0b\u65b9\uff0c\u67e5\u770b\u89e3\u51b3\u65b9\u6848\u3002" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "YHIWvc9Ms-Ll" } }, { "source": [ "cities['Is wide and has saint name'] = (cities['Area square miles'] > 50) & cities['City name'].apply(lambda name: name.startswith('San'))\n", "cities" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "T5OlrqtdtCIb", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ " ## \u7d22\u5f15\n", "`Series` \u548c `DataFrame` \u5bf9\u8c61\u4e5f\u5b9a\u4e49\u4e86 `index` \u5c5e\u6027\uff0c\u8be5\u5c5e\u6027\u4f1a\u5411\u6bcf\u4e2a `Series` \u9879\u6216 `DataFrame` \u884c\u8d4b\u4e00\u4e2a\u6807\u8bc6\u7b26\u503c\u3002\n", "\n", "\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5728\u6784\u9020\u65f6\uff0c*pandas* \u4f1a\u8d4b\u53ef\u53cd\u6620\u6e90\u6570\u636e\u987a\u5e8f\u7684\u7d22\u5f15\u503c\u3002\u7d22\u5f15\u503c\u5728\u521b\u5efa\u540e\u662f\u7a33\u5b9a\u7684\uff1b\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4eec\u4e0d\u4f1a\u56e0\u4e3a\u6570\u636e\u91cd\u65b0\u6392\u5e8f\u800c\u53d1\u751f\u6539\u53d8\u3002" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "f-xAOJeMiXFB" } }, { "source": [ "city_names.index" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "2684gsWNinq9", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ "cities.index" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "F_qPe2TBjfWd", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ " \u8c03\u7528 `DataFrame.reindex` \u4ee5\u624b\u52a8\u91cd\u65b0\u6392\u5217\u5404\u884c\u7684\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u65b9\u5f0f\u4e0e\u6309\u57ce\u5e02\u540d\u79f0\u6392\u5e8f\u5177\u6709\u76f8\u540c\u7684\u6548\u679c\uff1a" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "hp2oWY9Slo_h" } }, { "source": [ "cities.reindex([2, 0, 1])" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "sN0zUzSAj-U1", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ " \u91cd\u5efa\u7d22\u5f15\u662f\u4e00\u79cd\u968f\u673a\u6392\u5217 `DataFrame` \u7684\u7edd\u4f73\u65b9\u5f0f\u3002\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f1a\u53d6\u7528\u7c7b\u4f3c\u6570\u7ec4\u7684\u7d22\u5f15\uff0c\u7136\u540e\u5c06\u5176\u4f20\u9012\u81f3 NumPy \u7684 `random.permutation` \u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4f1a\u968f\u673a\u6392\u5217\u5176\u503c\u7684\u4f4d\u7f6e\u3002\u5982\u679c\u4f7f\u7528\u6b64\u91cd\u65b0\u968f\u673a\u6392\u5217\u7684\u6570\u7ec4\u8c03\u7528 `reindex`\uff0c\u4f1a\u5bfc\u81f4 `DataFrame` \u884c\u4ee5\u540c\u6837\u7684\u65b9\u5f0f\u968f\u673a\u6392\u5217\u3002\n", "\u5c1d\u8bd5\u591a\u6b21\u8fd0\u884c\u4ee5\u4e0b\u5355\u5143\u683c\uff01" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "-GQFz8NZuS06" } }, { "source": [ "cities.reindex(np.random.permutation(cities.index))" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "mF8GC0k8uYhz", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ " \u6709\u5173\u8be6\u60c5\uff0c\u8bf7\u53c2\u9605[\u7d22\u5f15\u6587\u6863](http://pandas.pydata.org/pandas-docs/stable/indexing.html#index-objects)\u3002" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "fSso35fQmGKb" } }, { "source": [ " ## \u7ec3\u4e60 2\n", "\n", "`reindex` \u65b9\u6cd5\u5141\u8bb8\u4f7f\u7528\u672a\u5305\u542b\u5728\u539f\u59cb `DataFrame` \u7d22\u5f15\u503c\u4e2d\u7684\u7d22\u5f15\u503c\u3002\u8bf7\u8bd5\u4e00\u4e0b\uff0c\u770b\u770b\u5982\u679c\u4f7f\u7528\u6b64\u7c7b\u503c\u4f1a\u53d1\u751f\u4ec0\u4e48\uff01\u60a8\u8ba4\u4e3a\u5141\u8bb8\u6b64\u7c7b\u503c\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "8UngIdVhz8C0" } }, { "source": [ "# Your code here" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "PN55GrDX0jzO", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ " ### \u89e3\u51b3\u65b9\u6848\n", "\n", "\u70b9\u51fb\u4e0b\u65b9\uff0c\u67e5\u770b\u89e3\u51b3\u65b9\u6848\u3002" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "TJffr5_Jwqvd" } }, { "source": [ " \u5982\u679c\u60a8\u7684 `reindex` \u8f93\u5165\u6570\u7ec4\u5305\u542b\u539f\u59cb `DataFrame` \u7d22\u5f15\u503c\u4e2d\u6ca1\u6709\u7684\u503c\uff0c`reindex` \u4f1a\u4e3a\u6b64\u7c7b\u201c\u4e22\u5931\u7684\u201d\u7d22\u5f15\u6dfb\u52a0\u65b0\u884c\uff0c\u5e76\u5728\u6240\u6709\u5bf9\u5e94\u5217\u4e2d\u586b\u5145 `NaN` \u503c\uff1a" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "8oSvi2QWwuDH" } }, { "source": [ "cities.reindex([0, 4, 5, 2])" ], "cell_type": "code", "metadata": { "colab_type": "code", "id": "yBdkucKCwy4x", "colab": { "autoexec": { "wait_interval": 0, "startup": false } } }, "outputs": [], "execution_count": 0 }, { "source": [ " \u8fd9\u79cd\u884c\u4e3a\u662f\u53ef\u53d6\u7684\uff0c\u56e0\u4e3a\u7d22\u5f15\u901a\u5e38\u662f\u4ece\u5b9e\u9645\u6570\u636e\u4e2d\u63d0\u53d6\u7684\u5b57\u7b26\u4e32\uff08\u8bf7\u53c2\u9605 [*pandas* reindex \u6587\u6863](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reindex.html)\uff0c\u67e5\u770b\u7d22\u5f15\u503c\u662f\u6d4f\u89c8\u5668\u540d\u79f0\u7684\u793a\u4f8b\uff09\u3002\n", "\n", "\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u5141\u8bb8\u51fa\u73b0\u201c\u4e22\u5931\u7684\u201d\u7d22\u5f15\uff0c\u60a8\u5c06\u53ef\u4ee5\u8f7b\u677e\u4f7f\u7528\u5916\u90e8\u5217\u8868\u91cd\u5efa\u7d22\u5f15\uff0c\u56e0\u4e3a\u60a8\u4e0d\u5fc5\u62c5\u5fc3\u4f1a\u5c06\u8f93\u5165\u6e05\u7406\u6389\u3002" ], "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "2l82PhPbwz7g" } } ], "metadata": { "colab": { "default_view": {}, "version": "0.3.2", "collapsed_sections": [ "JndnmDMp66FL", "YHIWvc9Ms-Ll", "TJffr5_Jwqvd" ], "name": "intro_to_pandas.ipynb", "views": {} }, "kernelspec": { "name": "python3", "display_name": "Python 3" } } }