{"id":222,"date":"2022-08-19T17:42:59","date_gmt":"2022-08-19T09:42:59","guid":{"rendered":"http:\/\/162.14.82.114\/?p=222"},"modified":"2022-08-21T17:22:46","modified_gmt":"2022-08-21T09:22:46","slug":"pandas%e5%9f%ba%e7%a1%80","status":"publish","type":"post","link":"http:\/\/162.14.82.114\/index.php\/222\/08\/19\/2022\/","title":{"rendered":"pandas\u5b66\u4e60\u8bb0\u5f55\uff08\u4e00\uff09"},"content":{"rendered":"<h2><strong>\u672c\u5b66\u4e60\u8bb0\u5f55\u53c2\u8003<code>Joyful-Pandas<\/code>\u8fdb\u884c\u5b66\u4e60<\/strong><\/h2>\n<h1>pandas\u57fa\u7840<\/h1>\n<h2>\u77e5\u8bc6\u70b9<\/h2>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208191742668.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208191742668.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\u672a\u547d\u540d\u6587\u4ef6\" style=\"zoom:80%;\" \/><\/div><\/p>\n<h2>\u95ee\u9898\u4e0e\u7ec3\u4e60<\/h2>\n<h3>1. \u95ee\u9898<\/h3>\n<h4>\u3010\u95ee\u9898\u4e00\u3011 Series\u548cDataFrame\u6709\u54ea\u4e9b\u5e38\u89c1\u5c5e\u6027\u548c\u65b9\u6cd5\uff1f<\/h4>\n<ul>\n<li>Series<\/li>\n<\/ul>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u5c5e\u6027\u65b9\u6cd5<\/th>\n<th style=\"text-align: center;\">\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">s.values<\/td>\n<td style=\"text-align: center;\">\u8bbf\u95ees\u7684\u5185\u5bb9<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">s.index<\/td>\n<td style=\"text-align: center;\">\u83b7\u53d6s\u7684\u7d22\u5f15<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">s.iteritems()<\/td>\n<td style=\"text-align: center;\">\u83b7\u53d6\u7d22\u5f15\u548c\u503c\u5bf9<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">s.dtype<\/td>\n<td style=\"text-align: center;\">\u83b7\u53d6s\u7684\u6570\u636e\u7c7b\u578b<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">s[\u2018a\u2019]<\/td>\n<td style=\"text-align: center;\">\u6839\u636e\u7d22\u5f15\u8bbf\u95ee\u5143\u7d20<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>DataFrame<\/li>\n<\/ul>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u5c5e\u6027\u65b9\u6cd5<\/th>\n<th style=\"text-align: center;\">\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">df.index<\/td>\n<td style=\"text-align: center;\">\u8bbf\u95ee\u884c\u7d22\u5f15<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">df.columns<\/td>\n<td style=\"text-align: center;\">\u8bbf\u95ee\u5217\u7d22\u5f15<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">df.values<\/td>\n<td style=\"text-align: center;\">\u8bbf\u95ee\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">df.shape<\/td>\n<td style=\"text-align: center;\">\u83b7\u53d6df\u7684\u6570\u636e\u5f62\u72b6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u3010\u95ee\u9898\u4e8c\u3011 value_counts\u4f1a\u7edf\u8ba1\u7f3a\u5931\u503c\u5417\uff1f<\/h4>\n<p>\u4e0d\u4f1a,\u7edf\u8ba1\u7f3a\u5931\u503c\u53ef\u4ee5\u4f7f\u7528<code>df[&#039;columns&#039;].isnull().value_counts()<\/code><\/p>\n<h4>\u3010\u95ee\u9898\u4e09\u3011 \u5982\u679c\u6709\u591a\u4e2a\u7d22\u5f15\u540c\u65f6\u53d6\u5230\u6700\u5927\u503c\uff0cidxmax\u4f1a\u8fd4\u56de\u6240\u6709\u8fd9\u4e9b\u7d22\u5f15\u5417\uff1f\u5982\u679c\u4e0d\u4f1a\uff0c\u90a3\u4e48\u600e\u4e48\u8fd4\u56de\u8fd9\u4e9b\u7d22\u5f15\uff1f<\/h4>\n<p>\u4e0d\u4f1a\uff0c\u8fd4\u56de\u7684\u662f\u7b2c\u4e00\u4e2a\u6700\u5927\u503c\u7684\u7d22\u5f15\uff1b<br \/>\n\u53ef\u4ee5\u4f7f\u7528<code>maxindex = s[s==s.max()].index<\/code><\/p>\n<h4>\u3010\u95ee\u9898\u56db\u3011 \u5728\u5e38\u7528\u51fd\u6570\u4e00\u8282\u4e2d\uff0c\u7531\u4e8e\u4e00\u4e9b\u51fd\u6570\u7684\u529f\u80fd\u6bd4\u8f83\u7b80\u5355\uff0c\u56e0\u6b64\u6ca1\u6709\u5217\u5165\uff0c\u73b0\u5728\u5c06\u5b83\u4eec\u5217\u5728\u4e0b\u9762\uff0c\u8bf7\u5206\u522b\u8bf4\u660e\u5b83\u4eec\u7684\u7528\u9014\u5e76\u5c1d\u8bd5\u4f7f\u7528\u3002<\/h4>\n<h4>sum\/mean\/median\/mad\/min\/max\/abs\/std\/var\/quantile\/cummax\/cumsum\/cumprod<\/h4>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u51fd\u6570<\/th>\n<th style=\"text-align: center;\">\u529f\u80fd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">sum<\/td>\n<td style=\"text-align: center;\">\u6c42\u548c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">mean<\/td>\n<td style=\"text-align: center;\">\u5747\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">median<\/td>\n<td style=\"text-align: center;\">\u4e2d\u4f4d\u6570<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">mad<\/td>\n<td style=\"text-align: center;\">\u5747\u503c\u5dee\u7684\u7edd\u5bf9\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">min<\/td>\n<td style=\"text-align: center;\">\u6700\u5c0f\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">max<\/td>\n<td style=\"text-align: center;\">\u6700\u5927\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">abs<\/td>\n<td style=\"text-align: center;\">\u7edd\u5bf9\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">std<\/td>\n<td style=\"text-align: center;\">\u6807\u51c6\u5dee<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">var<\/td>\n<td style=\"text-align: center;\">\u65b9\u5dee<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">quantile<\/td>\n<td style=\"text-align: center;\">\u5206\u4f4d\u6570<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">cummax<\/td>\n<td style=\"text-align: center;\">\u5230\u76ee\u524d\u4e3a\u6b62\u7684\u6700\u5927\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">cumsum<\/td>\n<td style=\"text-align: center;\">\u7d2f\u52a0\u548c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">cumprod<\/td>\n<td style=\"text-align: center;\">\u7d2f\u8ba1\u79ef<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u3010\u95ee\u9898\u4e94\u3011 df.mean(axis=1)\u662f\u4ec0\u4e48\u610f\u601d\uff1f\u5b83\u4e0edf.mean()\u7684\u7ed3\u679c\u4e00\u6837\u5417\uff1f\u95ee\u9898\u56db\u63d0\u5230\u7684\u51fd\u6570\u4e5f\u6709axis\u53c2\u6570\u5417\uff1f\u600e\u4e48\u4f7f\u7528\uff1f<\/h4>\n<p>df.mean(axis=1)\u662fdf\u4e2d\u884c\u7684\u5e73\u5747\u503c\uff0cdf.mean()\u662f\u5217\u7684\u5e73\u5747\u503c<\/p>\n<h4>\u3010\u95ee\u9898\u516d\u3011 \u5bf9\u503c\u8fdb\u884c\u6392\u5e8f\u540e\uff0c\u76f8\u540c\u7684\u503c\u6b21\u5e8f\u7531\u4ec0\u4e48\u51b3\u5b9a\uff1f<\/h4>\n<p>\u7531\u7d22\u5f15\u51b3\u5b9a<\/p>\n<h4>\u3010\u95ee\u9898\u4e03\u3011 Pandas\u4e2d\u4e3a\u5404\u7c7b\u57fa\u7840\u8fd0\u7b97\u4e5f\u5b9a\u4e49\u4e86\u51fd\u6570\uff0c\u6bd4\u5982s1.add(s2)\u8868\u793a\u4e24\u4e2aSeries\u76f8\u52a0\uff0c\u4f46\u65e2\u7136\u5df2\u7ecf\u6709\u4e86'+'\uff0c\u662f\u4e0d\u662f\u591a\u6b64\u4e00\u4e3e\uff1f<\/h4>\n<p>add()\u5305\u542b fill_value \u53c2\u6570\uff0c \u53ef\u4ee5\u586b\u5145\u7f3a\u5931\u503c\u7136\u540e\u518d\u76f8\u52a0<\/p>\n<h4>\u3010\u95ee\u9898\u516b\u3011\u5982\u679cDataFrame\u67d0\u4e00\u5217\u7684\u5143\u7d20\u662fnumpy\u6570\u7ec4\uff0c\u90a3\u4e48\u5c06\u5176\u4fdd\u5b58\u5230csv\u5728\u8bfb\u53d6\u540e\u5c31\u4f1a\u53d8\u6210\u5b57\u7b26\u4e32\uff0c\u600e\u4e48\u89e3\u51b3\uff1f<\/h4>\n<p>df['columns'] = df['columns'].astype(np.float32)<\/p>\n<h3>2. \u7ec3\u4e60<\/h3>\n<h4>\u3010\u7ec3\u4e60\u4e00\u3011 \u73b0\u6709\u4e00\u4efd\u5173\u4e8e\u7f8e\u5267\u300a\u6743\u529b\u7684\u6e38\u620f\u300b\u5267\u672c\u7684\u6570\u636e\u96c6\uff0c\u8bf7\u89e3\u51b3\u4ee5\u4e0b\u95ee\u9898\uff1a<\/h4>\n<h5>\uff08a\uff09\u5728\u6240\u6709\u7684\u6570\u636e\u4e2d\uff0c\u4e00\u5171\u51fa\u73b0\u4e86\u591a\u5c11\u4eba\u7269\uff1f<\/h5>\n<pre><code class=\"language-python\">df = pd.read_csv(&#039;data\/Game_of_Thrones_Script.csv&#039;)\ndf[&#039;Name&#039;].nunique()\n#564<\/code><\/pre>\n<h5>\uff08b\uff09\u4ee5\u5355\u5143\u683c\u8ba1\u6570\uff08\u5373\u7b80\u5355\u628a\u4e00\u4e2a\u5355\u5143\u683c\u89c6\u4f5c\u4e00\u53e5\uff09\uff0c\u8c01\u8bf4\u4e86\u6700\u591a\u7684\u8bdd\uff1f<\/h5>\n<pre><code class=\"language-python\">df[&#039;Name&#039;].value_counts()\n&#039;&#039;&#039;\ntyrion lannister      1760\njon snow              1133\ndaenerys targaryen    1048\ncersei lannister      1005\njaime lannister        945\n                      ... \nmerry                    1\nnights watchmen          1\nwillem                   1\ngreizhen                 1\nunsullied captain        1\nName: Name, Length: 564, dtype: int64\n&#039;&#039;&#039;<\/code><\/pre>\n<h5>\uff08c\uff09\u4ee5\u5355\u8bcd\u8ba1\u6570\uff0c\u8c01\u8bf4\u4e86\u6700\u591a\u7684\u5355\u8bcd\uff1f\uff08\u4e0d\u662f\u5355\u53e5\u5355\u8bcd\u6700\u591a\uff0c\u662f\u6307\u6bcf\u4eba\u8bf4\u8fc7\u5355\u8bcd\u7684\u603b\u6570\u6700\u591a\uff0c\u4e3a\u4e86\u7b80\u4fbf\uff0c\u53ea\u4ee5\u7a7a\u683c\u4e3a\u5355\u8bcd\u5206\u754c\u70b9\uff0c\u4e0d\u8003\u8651\u5176\u4ed6\u60c5\u51b5\uff09<\/h5>\n<pre><code class=\"language-python\">df[&#039;Sentence&#039;].str.len().value_counts().idxmax()\n#22<\/code><\/pre>\n<h4>\u3010\u7ec3\u4e60\u4e8c\u3011\u73b0\u6709\u4e00\u4efd\u5173\u4e8e\u79d1\u6bd4\u7684\u6295\u7bee\u6570\u636e\u96c6\uff0c\u8bf7\u89e3\u51b3\u5982\u4e0b\u95ee\u9898\uff1a<\/h4>\n<h5>\uff08a\uff09\u54ea\u79cdaction_type\u548ccombined_shot_type\u7684\u7ec4\u5408\u662f\u6700\u591a\u7684\uff1f<\/h5>\n<pre><code class=\"language-python\">df = pd.read_csv(&#039;data\/Kobe_data.csv&#039;,index_col=&#039;shot_id&#039;)\npd.Series(list(zip(df[&#039;action_type&#039;],df[&#039;combined_shot_type&#039;]))).value_counts()\n&#039;&#039;&#039;\n(Jump Shot, Jump Shot)                             18880\n(Layup Shot, Layup)                                 2567\n(Driving Layup Shot, Layup)                         1978\n                                                ........\n(Driving Floating Bank Jump Shot, Jump Shot)           1\n(Turnaround Fadeaway Bank Jump Shot, Jump Shot)        1\n(Cutting Finger Roll Layup Shot, Layup)                1\ndtype: int64\n&#039;&#039;&#039;<\/code><\/pre>\n<h5>\uff08b\uff09\u5728\u6240\u6709\u88ab\u8bb0\u5f55\u7684game_id\u4e2d\uff0c\u906d\u9047\u5230\u6700\u591a\u7684opponent\u662f\u4e00\u4e2a\u652f\uff1f\uff08\u7531\u4e8e\u4e00\u573a\u6bd4\u8d5b\u4f1a\u6709\u8bb8\u591a\u6b21\u6295\u7bee\uff0c\u4f46\u5bf9\u9635\u7684\u5bf9\u624b\u53ea\u6709\u4e00\u4e2a\uff0c\u672c\u9898\u76f8\u5f53\u4e8e\u95ee\u79d1\u6bd4\u548c\u54ea\u4e2a\u961f\u4ea4\u950b\u6b21\u6570\u6700\u591a\uff09<\/h5>\n<pre><code class=\"language-python\">df.groupby(&#039;game_id&#039;)[&#039;opponent&#039;].unique().astype(str).value_counts()\n&#039;&#039;&#039;\n[&#039;SAS&#039;]    91\n[&#039;PHX&#039;]    87\n[&#039;UTA&#039;]    84\n[&#039;DEN&#039;]    83\n   .........\n[&#039;VAN&#039;]    18\n[&#039;NOP&#039;]    16\n[&#039;BKN&#039;]     3\nName: opponent, dtype: int64\n&#039;&#039;&#039;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u5b66\u4e60\u8bb0\u5f55\u53c2\u8003Joyful-Pandas\u8fdb\u884c\u5b66\u4e60 pandas\u57fa\u7840 \u77e5\u8bc6\u70b9 \u95ee\u9898\u4e0e\u7ec3\u4e60 1. \u95ee\u9898 \u3010\u95ee\u9898\u4e00\u3011 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,32],"tags":[],"class_list":["post-222","post","type-post","status-publish","format-standard","hentry","category-artificial-intelligence","category-python-learn"],"_links":{"self":[{"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/posts\/222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/comments?post=222"}],"version-history":[{"count":4,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/posts\/222\/revisions"}],"predecessor-version":[{"id":235,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/posts\/222\/revisions\/235"}],"wp:attachment":[{"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/media?parent=222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/categories?post=222"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/tags?post=222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}