{"id":2940,"date":"2019-01-03T10:11:00","date_gmt":"2019-01-03T02:11:00","guid":{"rendered":"http:\/\/shanlin.info\/?p=2940"},"modified":"2021-04-25T19:47:51","modified_gmt":"2021-04-25T11:47:51","slug":"pandas-dataframe%e7%9a%84loc%e3%80%81iloc%e3%80%81ix%e5%92%8cat-iat%e6%b5%85%e6%9e%90","status":"publish","type":"post","link":"https:\/\/shanlin.info\/?p=2940","title":{"rendered":"Pandas DataFrame\u7684loc\u3001iloc\u3001ix\u548cat\/iat\u6d45\u6790"},"content":{"rendered":"\n<p>\u524d\u6bb5\u65f6\u95f4\u770bThink Python\u91cc\u9762\u6709\u53e5\u8bdd\u8bb0\u5fc6\u72b9\u65b0\uff0c\u5927\u6982\u610f\u601d\u662f\uff1a\u6709\u65f6\u5019Python\u8ba9\u6211\u4eec\u611f\u5230\u56f0\u60d1\uff0c\u662f\u56e0\u4e3a\u5b9e\u73b0\u4e00\u4e2a\u6548\u679c\u7684\u65b9\u6cd5\u592a\u591a\uff0c\u800c\u4e0d\u662f\u592a\u5c11\u3002<\/p>\n\n\n\n<p>\u786e\u5b9e\u5982\u6b64\uff0cPandas\u7684DataFrame\u6570\u636e\u9009\u53d6\u5c31\u5b58\u5728\u8fd9\u6837\u7684\u95ee\u9898\u3002\u672c\u6765\u7406\u89e3\u5217\u8868\u7d22\u5f15\uff08\u4e86\u89e3\u5217\u8868\u7d22\u5f15\u8bf7\u53c2\u8003\uff1a<a href=\"https:\/\/www.jianshu.com\/p\/a98e935e4d46\" target=\"_blank\" rel=\"noreferrer noopener\">\u4e00\u5f20\u56fe\u5f04\u61c2python\u7d22\u5f15\u548c\u5207\u7247<\/a>\uff09\u5c31\u5df2\u7ecf\u5f88\u56f0\u96be\u4e86\uff0cDataFrame\u8fd8\u5e26\u8fd9\u4e48\u591a\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u5e9f\u8bdd\u5c11\u8bf4\uff0c\u76f4\u63a5\u4e0a\u7ed3\u679c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1\u3001loc\uff1a\u901a\u8fc7\u6807\u7b7e\u9009\u53d6\u6570\u636e\uff0c\u5373\u901a\u8fc7index\u548ccolumns\u7684\u503c\u8fdb\u884c\u9009\u53d6\u3002loc\u65b9\u6cd5\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u6309\u987a\u5e8f\u63a7\u5236\u884c\u5217\u9009\u53d6\u3002<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u793a\u4f8b\u6570\u636e\u96c6\ndf=pd.DataFrame(np.arange(12).reshape(4,3),columns=list('abc'),index=list('defg'))\n\ndf\nOut&#91;189]: \n   a   b   c\nd  0   1   2\ne  3   4   5\nf  6   7   8\ng  9  10  11\n\n#\u76f4\u63a5\u7d22\u5f15\u884c\ndf.loc&#91;'d']\nOut&#91;190]: \na    0\nb    1\nc    2\nName: d, dtype: int32\n\n#\u7d22\u5f15\u591a\u884c\ndf.loc&#91;&#91;'d','e']]\nOut&#91;191]: \n   a  b  c\nd  0  1  2\ne  3  4  5\n\n#\u7d22\u5f15\u591a\u5217\ndf.loc&#91;:,:'b']\nOut&#91;193]: \n   a   b\nd  0   1\ne  3   4\nf  6   7\ng  9  10\n\n#\u5982\u679c\u7d22\u5f15\u7684\u6807\u7b7e\u4e0d\u5728index\u6216columns\u8303\u56f4\u5219\u4f1a\u62a5\u9519\uff0ca\u6807\u7b7e\u5728\u5217\u4e2d\uff0cloc\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u884c\u7d22\u5f15\u3002\ndf.loc&#91;'a']\nTraceback (most recent call last):\n\u2026\u2026\nKeyError: 'the label &#91;a] is not in the &#91;index]'\n\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2\u3001iloc\uff1a\u901a\u8fc7\u884c\u53f7\u9009\u53d6\u6570\u636e\uff0c\u5373\u901a\u8fc7\u6570\u636e\u6240\u5728\u7684\u81ea\u7136\u884c\u5217\u6570\u4e3a\u9009\u53d6\u6570\u636e\u3002iloc\u65b9\u6cd5\u4e5f\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u6309\u987a\u5e8f\u63a7\u5236\u884c\u5217\u9009\u53d6\u3002<\/h4>\n\n\n\n<p>\u6ce8\u610f\uff1a\u884c\u53f7\u548c\u7d22\u5f15\u6709\u6240\u5dee\u5f02\uff0c\u8fdb\u884c\u7b5b\u9009\u540e\u7684\u6570\u636e\u884c\u53f7\u4f1a\u6839\u636e\u65b0\u7684DataFrame\u53d8\u5316\uff0c\u800c\u7d22\u5f15\u4e0d\u4f1a\u53d1\u751f\u53d8\u5316\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>df\nOut&#91;196]: \n   a   b   c\nd  0   1   2\ne  3   4   5\nf  6   7   8\ng  9  10  11\n\n#\u9009\u53d6\u4e00\u884c\ndf.iloc&#91;0]\nOut&#91;197]: \na    0\nb    1\nc    2\nName: d, dtype: int32\n\n#\u9009\u53d6\u591a\u884c\ndf.iloc&#91;0:2]\nOut&#91;198]: \n   a  b  c\nd  0  1  2\ne  3  4  5\n\n#\u9009\u53d6\u4e00\u5217\u6216\u591a\u5217\ndf.iloc&#91;:,2:3]\nOut&#91;199]: \n    c\nd   2\ne   5\nf   8\ng  11\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3\u3001ix\uff1a\u6df7\u5408\u7d22\u5f15\uff0c\u540c\u65f6\u901a\u8fc7\u6807\u7b7e\u548c\u884c\u53f7\u9009\u53d6\u6570\u636e\u3002ix\u65b9\u6cd5\u4e5f\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u6309\u987a\u5e8f\u63a7\u5236\u884c\u5217\u9009\u53d6\u3002<\/h4>\n\n\n\n<p>\u6ce8\u610f\uff1aix\u7684\u4e24\u4e2a\u53c2\u6570\u4e2d\uff0c\u6bcf\u4e2a\u53c2\u6570\u5728\u7d22\u5f15\u65f6\u5fc5\u987b\u4fdd\u6301\u53ea\u4f7f\u7528\u6807\u7b7e\u6216\u884c\u53f7\u8fdb\u884c\u6570\u636e\u9009\u53d6\uff0c\u5426\u5219\u4f1a\u8fd4\u56de\u4e00\u90e8\u5206\u63a7\u5236\u7ed3\u679c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>df\nOut&#91;200]: \n   a   b   c\nd  0   1   2\ne  3   4   5\nf  6   7   8\ng  9  10  11\n\n#\u9009\u53d6\u4e00\u884c\ndf.ix&#91;1]\nOut&#91;201]: \na    3\nb    4\nc    5\nName: e, dtype: int32\n\n#\u9519\u8bef\u7684\u6df7\u5408\u7d22\u5f15\uff08\u60f3\u9009\u53d6\u7b2c\u4e00\u884c\u548ce\u884c\uff09\ndf.ix&#91;&#91;0,'e']]\nOut&#91;202]: \n     a    b    c\n0  NaN  NaN  NaN\ne  3.0  4.0  5.0\n\n#\u9009\u53d6\u533a\u57df\uff08e\u884c\u7684\u524d\u4e24\u5217\uff09\ndf.ix&#91;'e':,:2]\nOut&#91;203]: \n   a   b\ne  3   4\nf  6   7\ng  9  10\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4\u3001at\/iat\uff1a\u901a\u8fc7\u6807\u7b7e\u6216\u884c\u53f7\u83b7\u53d6\u67d0\u4e2a\u6570\u503c\u7684\u5177\u4f53\u4f4d\u7f6e\u3002<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>df\nOut&#91;204]: \n   a   b   c\nd  0   1   2\ne  3   4   5\nf  6   7   8\ng  9  10  11\n\n#\u83b7\u53d6\u7b2c2\u884c\uff0c\u7b2c3\u5217\u4f4d\u7f6e\u7684\u6570\u636e\ndf.iat&#91;1,2]\nOut&#91;205]: 5\n\n#\u83b7\u53d6f\u884c\uff0ca\u5217\u4f4d\u7f6e\u7684\u6570\u636e\ndf.at&#91;'f','a']\nOut&#91;206]: 6\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">5\u3001\u76f4\u63a5\u7d22\u5f15 df[]<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>df\nOut&#91;208]: \n   a   b   c\nd  0   1   2\ne  3   4   5\nf  6   7   8\ng  9  10  11\n\n#\u9009\u53d6\u884c\ndf&#91;0:3]\nOut&#91;209]: \n   a  b  c\nd  0  1  2\ne  3  4  5\nf  6  7  8\n\n#\u9009\u53d6\u5217\ndf&#91;'a']\nOut&#91;210]: \nd    0\ne    3\nf    6\ng    9\nName: a, dtype: int32\n\n#\u9009\u53d6\u591a\u5217\ndf&#91;&#91;'a','c']]\nOut&#91;211]: \n   a   c\nd  0   2\ne  3   5\nf  6   8\ng  9  11\n\n#\u884c\u53f7\u548c\u533a\u95f4\u7d22\u5f15\u53ea\u80fd\u7528\u4e8e\u884c\uff08\u9884\u60f3\u9009\u53d6C\u5217\u7684\u6570\u636e\uff0c\n#\u4f46\u8fd9\u91cc\u9009\u53d6\u9664\u4e86df\u7684\u6240\u6709\u6570\u636e\uff0c\u533a\u95f4\u7d22\u5f15\u53ea\u80fd\u7528\u4e8e\u884c\uff0c\n#\u56e0defg\u5747&gt;c\uff0c\u6240\u4ee5\u6240\u6709\u884c\u5747\u88ab\u9009\u53d6\u51fa\u6765\uff09\ndf&#91;'c':]\nOut&#91;212]: \n   a   b   c\nd  0   1   2\ne  3   4   5\nf  6   7   8\ng  9  10  11\ndf&#91;'f':]\nOut&#91;213]: \n   a   b   c\nf  6   7   8\ng  9  10  11\n\n#df.\u9009\u53d6\u5217\ndf.a\nOut&#91;214]: \nd    0\ne    3\nf    6\ng    9\nName: a, dtype: int32\n#\u4e0d\u80fd\u4f7f\u7528df.\u9009\u62e9\u884c\ndf.f\nTraceback (most recent call last):\n  File \"&lt;ipython-input-215-6438703abe20&gt;\", line 1, in &lt;module&gt;\n    df.f\n  File \"C:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\generic.py\", line 2744, in __getattr__\n    return object.__getattribute__(self, name)\nAttributeError: 'DataFrame' object has no attribute 'f'\n\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">6\u3001\u603b\u7ed3<\/h4>\n\n\n\n<p>1\uff09.loc,.iloc,.ix,\u53ea\u52a0\u7b2c\u4e00\u4e2a\u53c2\u6570\u5982.loc([1,2]),.iloc([2:3]),.ix[2]\u2026\u5219\u8fdb\u884c\u7684\u662f\u884c\u9009\u62e9<br>2\uff09.loc,.at\uff0c\u9009\u5217\u662f\u53ea\u80fd\u662f\u5217\u540d\uff0c\u4e0d\u80fd\u662fposition<br>3\uff09.iloc,.iat\uff0c\u9009\u5217\u662f\u53ea\u80fd\u662fposition\uff0c\u4e0d\u80fd\u662f\u5217\u540d<br>4\uff09df[]\u53ea\u80fd\u8fdb\u884c\u884c\u9009\u62e9\uff0c\u6216\u5217\u9009\u62e9\uff0c\u4e0d\u80fd\u540c\u65f6\u8fdb\u884c\u5217\u9009\u62e9\uff0c\u5217\u9009\u62e9\u53ea\u80fd\u662f\u5217\u540d\u3002\u884c\u53f7\u548c\u533a\u95f4\u9009\u62e9\u53ea\u80fd\u8fdb\u884c\u884c\u9009\u62e9\u3002\u5f53index\u548ccolumns\u6807\u7b7e\u503c\u5b58\u5728\u91cd\u590d\u65f6\uff0c\u901a\u8fc7\u6807\u7b7e\u9009\u62e9\u4f1a\u4f18\u5148\u8fd4\u56de\u884c\u6570\u636e\u3002df.\u53ea\u80fd\u8fdb\u884c\u5217\u9009\u62e9\uff0c\u4e0d\u80fd\u8fdb\u884c\u884c\u9009\u62e9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u6bb5\u65f6\u95f4\u770bThink Python\u91cc\u9762\u6709\u53e5\u8bdd\u8bb0\u5fc6\u72b9\u65b0\uff0c\u5927\u6982\u610f\u601d\u662f\uff1a\u6709\u65f6\u5019Python\u8ba9\u6211\u4eec\u611f\u5230\u56f0\u60d1\uff0c\u662f\u56e0\u4e3a\u5b9e\u73b0<\/p><\/div>\n<div class=\"blog-btn\"><a href=\"https:\/\/shanlin.info\/?p=2940\" class=\"home-blog-btn\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts\/2940"}],"collection":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2940"}],"version-history":[{"count":2,"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts\/2940\/revisions"}],"predecessor-version":[{"id":3110,"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts\/2940\/revisions\/3110"}],"wp:attachment":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}