{"id":974,"date":"2022-01-10T21:37:05","date_gmt":"2022-01-10T13:37:05","guid":{"rendered":"http:\/\/www.eait.co\/?p=974"},"modified":"2022-01-11T17:47:18","modified_gmt":"2022-01-11T09:47:18","slug":"numpy%e6%95%b0%e7%bb%84-%e5%92%8c-dataframe%e5%88%9b%e5%bb%ba%e4%bf%ae%e6%94%b9","status":"publish","type":"post","link":"https:\/\/notes.coremix.net\/?p=974","title":{"rendered":"numpy\u6570\u7ec4 \u548c DataFrame\u521b\u5efa\u4fee\u6539"},"content":{"rendered":"<p>\u6570\u7ec4\uff0cpandas\u521b\u5efa&#038;\u5408\u5e76<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport numpy as np\r\na = &#x5B;1,2,3,4,5,6]\r\na_array = np.array(a)\r\nprint(a_array,'a_array')\r\nprint(a_array*2, '**2')\r\ntwod = np.array(&#x5B;&#x5B;1,2],&#x5B;2,3],&#x5B;4,5]])\r\nprint(twod) # \u4e09\u884c\u4e24\u5217\r\n# np.arange  # \u521b\u5efa\u6709\u5e8f\u5217\u8868, \u7c7b\u4f3crange\r\nprint(np.arange(0,1,0.1))\r\n#np.random.randn(3)\u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u670d\u4ece\u6b63\u6001\u5206\u5e03\uff08\u5747\u503c\u4e3a0\u3001\u6807\u51c6\u5dee\u4e3a1\u7684\u5206\u5e03\uff09\u76843\u4e2a\u968f\u673a\u6570\r\n<\/pre>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# \u4e09\u884c\u56db\u5217\r\nprint(np.arange(12).reshape(3,4))\r\nprint(np.arange(12).reshape(3,4)*2)\r\n# \u968f\u673a\u6574\u6570\u6570\u7ec4:\r\nprint(np.random.randint(0, 10, (4, 5)))\r\n<\/pre>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\r\n# pandas\r\nimport pandas as pd\r\n# Series\r\nprint(pd.Series(&#x5B;'\u4e01\u4e00', '\u738b\u4e8c', '\u5f20\u4e09']))\r\nprint(pd.Series(&#x5B;'\u4e01\u4e00', '\u738b\u4e8c', '\u5f20\u4e09'],&#x5B;'\u4e01\u4e00', '\u738b\u4e8c', '\u5f20\u4e09']))\r\nprint(pd.Series(&#x5B;'\u4e01\u4e00', '\u738b\u4e8c', '\u5f20\u4e09'],&#x5B;'\u4e01\u4e00', '\u738b\u4e8c', '\u5f20\u4e09'])&#x5B;1])\r\n\r\n# \u521b\u5efadatafram\r\nprint(pd.DataFrame(np.arange(6).reshape(3,2)))\r\nprint(pd.DataFrame(&#x5B;&#x5B;1,2],&#x5B;2,3],&#x5B;4,5]]))\r\nprint(pd.DataFrame(&#x5B;&#x5B;1,2],&#x5B;2,3],&#x5B;4,5]]))\r\n\r\n\r\n# \u81ea\u5b9a\u4e49\u884c\u7d22\u5f15\u548c\u5217\u7d22\u5f15\r\na = pd.DataFrame(&#x5B;&#x5B;1, 2], &#x5B;3, 4], &#x5B;5, 6]], columns=&#x5B;'date', 'score'],\r\nindex=&#x5B;'A', 'B', 'C'])\r\nprint(a)\r\nprint(a&#x5B;'score']&#x5B;'A'])\r\n\r\n# \u901a\u8fc7\u81ea\u5b9a\u4e49\u5217\u521b\u5efa  # \u7b2c\u4e00\u4e2a\u952e\u662f\u5217,\u7b2c\u4e8c\u4e2a\u662f\u884c\r\ndate = &#x5B;1, 3, 5]\r\nscore = &#x5B;2, 4, 6]\r\nb = pd.DataFrame() # \u521b\u5efa\u4e00\u4e2a\u7a7aDataFrame\r\nb&#x5B;'date'] = date\r\nb&#x5B;'score'] = score\r\nprint(b)\r\nprint(pd.DataFrame(&#x5B;&#x5B;1,2],&#x5B;2,3],&#x5B;4,5]])&#x5B;1]&#x5B;1])\r\n\r\n# \u901a\u8fc7\u5b57\u5178\u521b\u5efa: key \u4e3a\u5b57\u6bb5\u540d\r\nb = pd.DataFrame({'a': &#x5B;1, 3, 5], 'b': &#x5B;2, 4, 6]}, index=&#x5B;'x',\r\n'y', 'z'])\r\nprint(b)\r\n# \u5c06key\u8bbe\u7f6e\u4e3a\u884c\u53f7:\r\nprint(pd.DataFrame.from_dict({'a': &#x5B;1, 3, 5], 'b': &#x5B;2, 4, 6]}, orient='index'))\r\n\r\n# \u4e8c\u7ef4\u6570\u7ec4\u521b\u5efa\u884c\u5217\u7d22\u5f15\r\na = np.arange(12).reshape(3, 4)\r\nc = pd.DataFrame(a, index=&#x5B;1, 2, 3], columns=&#x5B;'A', 'B', 'C', 'D'])\r\nprint(c)\r\n\r\n# \u91cd\u547d\u540d\u5b57\u6bb5\u548c\u884c\u53f7: \u521b\u5efa\u4e86\u65b0\u7684\u6570\u7ec4,\u4e5f\u53ef\u901a\u8fc7inplace=True\u6765\u8282\u7701\u5185\u5b58(\u5927\u8868\u9002\u7528)\r\na = pd.DataFrame(np.arange(6).reshape(3, 2))\r\na = a.rename(index={0:'\u4e07\u79d1', 1:'\u963f\u91cc', 2:'\u767e\u5ea6'}, columns=\r\n{0:'\u65e5\u671f', 1:'\u5206\u6570'})\r\nprint(a)\r\nprint(a.reset_index()) # \u884c\u7d22\u5f15\u91cd\u7f6e\u4e3a\u4e00\u5217,\u5e76\u589e\u52a0\u884c\u5e8f\u53f7\u7d22\u5f15\r\na = a.set_index('\u65e5\u671f',inplace=True)   # \u8f6c\u6362\u4e3a\u7d22\u5f15\u5217    \u5e76\u4e22\u5931\u4e86\u539f\u7d22\u5f15\r\nprint(a)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u7ec4\uff0cpandas\u521b\u5efa&#038;\u5408\u5e76 import numpy as np a = &#x5B;1,2,3, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,23],"tags":[20],"class_list":["post-974","post","type-post","status-publish","format-standard","hentry","category-python","category-python_note","tag-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=974"}],"version-history":[{"count":2,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/974\/revisions"}],"predecessor-version":[{"id":976,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/974\/revisions\/976"}],"wp:attachment":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}