{"id":204,"date":"2021-09-15T22:34:15","date_gmt":"2021-09-15T14:34:15","guid":{"rendered":"\/?p=204"},"modified":"2021-09-19T21:13:42","modified_gmt":"2021-09-19T13:13:42","slug":"pandas%e7%9a%84apply%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"\/?p=204","title":{"rendered":"Pandas\u7684apply\u65b9\u6cd5"},"content":{"rendered":"<p>\u5728\u5904\u7406\u80a1\u7968\u6570\u636e\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u628a\u4e00\u4e9b\u51fd\u6570\u5e94\u7528\u4e8e\u591a\u884c\u6216\u8005\u591a\u5217\u6570\u636e\u4e0a\uff0c\u8fd9\u65f6\u5c31\u9700\u8981\u4f7f\u7528Series\u548cDataFrame\u7684apply\u65b9\u6cd5\u3002\u672c\u6587\u4ee5sz.002407\u591a\u6c1f\u591a\u57282021\u5e74\u7684\u524d10\u6839\u65e5\u7ebf\u6570\u636e\uff08\u5982\u4e0b\u8868\u6240\u793a\uff09\u4e3a\u4f8b\uff0c\u5c55\u793aapply\u65b9\u6cd5\u7684\u4f7f\u7528\uff0c\u6211\u4eec\u9996\u5148\u5c06\u6570\u636e\u52a0\u8f7d\u5230\u4e00\u4e2a\u7c7b\u578b\u4e3aDataFrame\u7684\u5bf9\u8c61df\u4e2d\u3002<\/p>\n<div class=\"table-wrapper\">\n<table class=\"table table-bordered table-hover table-condensed\">\n<thead>\n<tr>\n<th title=\"Field #1\">date<\/th>\n<th title=\"Field #2\">open<\/th>\n<th title=\"Field #3\">high<\/th>\n<th title=\"Field #4\">low<\/th>\n<th title=\"Field #5\">close<\/th>\n<th title=\"Field #6\">preclose<\/th>\n<th title=\"Field #7\">volume<\/th>\n<th title=\"Field #8\">amount<\/th>\n<th title=\"Field #9\">adjustflag<\/th>\n<th title=\"Field #10\">turn<\/th>\n<th title=\"Field #11\">tradestatus<\/th>\n<th title=\"Field #12\">pctChg<\/th>\n<th title=\"Field #13\">peTTM<\/th>\n<th title=\"Field #14\">pbMRQ<\/th>\n<th title=\"Field #15\">psTTM<\/th>\n<th title=\"Field #16\">pcfNcfTTM<\/th>\n<th title=\"Field #17\">isST<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>2021-01-04<\/td>\n<td align=\"right\">20.0300000000<\/td>\n<td align=\"right\">21.4000000000<\/td>\n<td align=\"right\">19.9300000000<\/td>\n<td align=\"right\">20.7300000000<\/td>\n<td align=\"right\">20.0300000000<\/td>\n<td align=\"right\">69639314<\/td>\n<td align=\"right\">1442558998.1400<\/td>\n<td align=\"right\">2<\/td>\n<td align=\"right\">12.026700<\/td>\n<td>1<\/td>\n<td align=\"right\">3.494800<\/td>\n<td align=\"right\">-28.520577<\/td>\n<td align=\"right\">5.001054<\/td>\n<td align=\"right\">3.874122<\/td>\n<td align=\"right\">-336.943766<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>2021-01-05<\/td>\n<td align=\"right\">20.5100000000<\/td>\n<td align=\"right\">21.3100000000<\/td>\n<td align=\"right\">19.9500000000<\/td>\n<td align=\"right\">20.6800000000<\/td>\n<td align=\"right\">20.7300000000<\/td>\n<td align=\"right\">68155914<\/td>\n<td align=\"right\">1404877488.0600<\/td>\n<td align=\"right\">2<\/td>\n<td align=\"right\">11.770500<\/td>\n<td>1<\/td>\n<td align=\"right\">-0.241200<\/td>\n<td align=\"right\">-28.451786<\/td>\n<td align=\"right\">4.988992<\/td>\n<td align=\"right\">3.864778<\/td>\n<td align=\"right\">-336.131070<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>2021-01-06<\/td>\n<td align=\"right\">20.8300000000<\/td>\n<td align=\"right\">21.2800000000<\/td>\n<td align=\"right\">19.0600000000<\/td>\n<td align=\"right\">19.7700000000<\/td>\n<td align=\"right\">20.6800000000<\/td>\n<td align=\"right\">69108077<\/td>\n<td align=\"right\">1375667187.1400<\/td>\n<td align=\"right\">2<\/td>\n<td align=\"right\">11.935000<\/td>\n<td>1<\/td>\n<td align=\"right\">-4.400400<\/td>\n<td align=\"right\">-27.199798<\/td>\n<td align=\"right\">4.769457<\/td>\n<td align=\"right\">3.694713<\/td>\n<td align=\"right\">-321.340003<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>2021-01-07<\/td>\n<td align=\"right\">19.5500000000<\/td>\n<td align=\"right\">20.4400000000<\/td>\n<td align=\"right\">19.2100000000<\/td>\n<td align=\"right\">19.8900000000<\/td>\n<td align=\"right\">19.7700000000<\/td>\n<td align=\"right\">57333679<\/td>\n<td align=\"right\">1145163382.0700<\/td>\n<td align=\"right\">2<\/td>\n<td align=\"right\">9.901500<\/td>\n<td>1<\/td>\n<td align=\"right\">0.607000<\/td>\n<td align=\"right\">-27.364895<\/td>\n<td align=\"right\">4.798406<\/td>\n<td align=\"right\">3.717139<\/td>\n<td align=\"right\">-323.290473<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>2021-01-08<\/td>\n<td align=\"right\">19.8900000000<\/td>\n<td align=\"right\">20.1300000000<\/td>\n<td align=\"right\">18.6000000000<\/td>\n<td align=\"right\">19.4900000000<\/td>\n<td align=\"right\">19.8900000000<\/td>\n<td align=\"right\">55294505<\/td>\n<td align=\"right\">1063035986.1300<\/td>\n<td align=\"right\">2<\/td>\n<td align=\"right\">9.549300<\/td>\n<td>1<\/td>\n<td align=\"right\">-2.011100<\/td>\n<td align=\"right\">-26.814570<\/td>\n<td align=\"right\">4.701908<\/td>\n<td align=\"right\">3.642385<\/td>\n<td align=\"right\">-316.788905<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>2021-01-11<\/td>\n<td align=\"right\">18.9000000000<\/td>\n<td align=\"right\">18.9000000000<\/td>\n<td align=\"right\">17.5400000000<\/td>\n<td align=\"right\">17.5500000000<\/td>\n<td align=\"right\">19.4900000000<\/td>\n<td align=\"right\">77040397<\/td>\n<td align=\"right\">1371939544.2400<\/td>\n<td align=\"right\">2<\/td>\n<td align=\"right\">13.304900<\/td>\n<td>1<\/td>\n<td align=\"right\">-9.953800<\/td>\n<td align=\"right\">-24.145496<\/td>\n<td align=\"right\">4.233888<\/td>\n<td align=\"right\">3.279829<\/td>\n<td align=\"right\">-285.256300<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>2021-01-12<\/td>\n<td align=\"right\">17.2000000000<\/td>\n<td align=\"right\">18.3500000000<\/td>\n<td align=\"right\">16.8300000000<\/td>\n<td align=\"right\">18.1300000000<\/td>\n<td align=\"right\">17.5500000000<\/td>\n<td align=\"right\">55091872<\/td>\n<td align=\"right\">970628000.0300<\/td>\n<td align=\"right\">2<\/td>\n<td align=\"right\">9.514400<\/td>\n<td>1<\/td>\n<td align=\"right\">3.304800<\/td>\n<td align=\"right\">-24.943466<\/td>\n<td align=\"right\">4.373811<\/td>\n<td align=\"right\">3.388222<\/td>\n<td align=\"right\">-294.683573<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>2021-01-13<\/td>\n<td align=\"right\">18.2000000000<\/td>\n<td align=\"right\">19.5300000000<\/td>\n<td align=\"right\">18.2000000000<\/td>\n<td align=\"right\">18.7600000000<\/td>\n<td align=\"right\">18.1300000000<\/td>\n<td align=\"right\">68535355<\/td>\n<td align=\"right\">1303591461.9500<\/td>\n<td align=\"right\">2<\/td>\n<td align=\"right\">11.836000<\/td>\n<td>1<\/td>\n<td align=\"right\">3.474900<\/td>\n<td align=\"right\">-25.810228<\/td>\n<td align=\"right\">4.525797<\/td>\n<td align=\"right\">3.505959<\/td>\n<td align=\"right\">-304.923543<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>2021-01-14<\/td>\n<td align=\"right\">18.6900000000<\/td>\n<td align=\"right\">19.6200000000<\/td>\n<td align=\"right\">18.1200000000<\/td>\n<td align=\"right\">19.4000000000<\/td>\n<td align=\"right\">18.7600000000<\/td>\n<td align=\"right\">58518764<\/td>\n<td align=\"right\">1109560714.8600<\/td>\n<td align=\"right\">2<\/td>\n<td align=\"right\">10.106200<\/td>\n<td>1<\/td>\n<td align=\"right\">3.411500<\/td>\n<td align=\"right\">-26.690747<\/td>\n<td align=\"right\">4.680195<\/td>\n<td align=\"right\">3.625566<\/td>\n<td align=\"right\">-315.326052<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>2021-01-15<\/td>\n<td align=\"right\">19.0700000000<\/td>\n<td align=\"right\">19.6800000000<\/td>\n<td align=\"right\">18.1300000000<\/td>\n<td align=\"right\">19.3000000000<\/td>\n<td align=\"right\">19.4000000000<\/td>\n<td align=\"right\">55752774<\/td>\n<td align=\"right\">1052651676.0200<\/td>\n<td align=\"right\">2<\/td>\n<td align=\"right\">9.628500<\/td>\n<td>1<\/td>\n<td align=\"right\">-0.515500<\/td>\n<td align=\"right\">-26.553166<\/td>\n<td align=\"right\">4.656071<\/td>\n<td align=\"right\">3.606877<\/td>\n<td align=\"right\">-313.700660<\/td>\n<td>0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<hr \/>\n<h1>Series\u7684apply\u65b9\u6cd5<\/h1>\n<p>\u5728Pandas\u4e2d\uff0c\u4eceDataFrame\u53d6\u51fa1\u884c\u6216\u80051\u5217\uff0c\u90fd\u4f1a\u8fd4\u56deSeries\u5bf9\u8c61\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\"># \u53d61\u5217\u6570\u636e\nprint(type(df['date']))\n<\/code><\/pre>\n<pre><code class=\"language-bash line-numbers\">&lt;class 'pandas.core.series.Series'&gt;\n<\/code><\/pre>\n<pre><code class=\"language-python line-numbers\"># \u53d61\u884c\u6570\u636e\nprint(type(df.iloc[0]))\n<\/code><\/pre>\n<pre><code class=\"language-bash line-numbers\">&lt;class 'pandas.core.series.Series'&gt;\n<\/code><\/pre>\n<p>\u6211\u4eec\u4ee5date\u5217\u4e3a\u4f8b\uff0c\u6765\u4ecb\u7ecdSeries\u7684apply\u65b9\u6cd5\u7684\u4f7f\u7528\u3002<\/p>\n<p>\u5728\u539f\u59cb\u7684df\u4e2d\uff0cdate\u5217\u7684\u5143\u7d20\u7c7b\u578b\u5747\u4e3astr\uff0c\u6211\u4eec\u9996\u5148\u5c1d\u8bd5\u53bb\u6389\u5e74\u6708\u65e5\u4e4b\u95f4\u7684&#8221;-&#8220;\u7b26\u53f7\uff0c\u5373\u5c06&#8221;2021-01-04&#8243;\u8f6c\u5316\u4e3a&#8221;20210104&#8243;\u3002<\/p>\n<p>\u9996\u5148\u6253\u5370\u4e0b\u539f\u59cb\u7684date\u5217\u770b\u4e0b\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">print(df['date'])\n<\/code><\/pre>\n<pre><code class=\"language-bash line-numbers\">0    2021-01-04\n1    2021-01-05\n2    2021-01-06\n3    2021-01-07\n4    2021-01-08\n5    2021-01-11\n6    2021-01-12\n7    2021-01-13\n8    2021-01-14\n9    2021-01-15\nName: date, dtype: object\n<\/code><\/pre>\n<p>\u4e0b\u9762\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u8f93\u5165\u53c2\u6570\u4e3a\u539f\u59cb\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u503c\u4e3a\u66ff\u6362\u540e\u7684\u5b57\u7b26\u4e32\uff0c\u51fd\u6570\u5185\u90e8\u5b9e\u9645\u8c03\u7528\u7684\u662fstr\u7c7b\u7684replace\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">def str_replace(s):\n    return s.replace('-', '')\n<\/code><\/pre>\n<p>\u63a5\u7740\u6211\u4eec\u5c06\u81ea\u5b9a\u4e49\u7684str_replace\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9Series\u7684apply\u65b9\u6cd5\uff0c\u5c31\u53ef\u4ee5\u5e94\u7528\u5230Series\u7684\u6bcf\u4e2a\u5143\u7d20\u4e0a\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">sr = df['date'].apply(str_replace)\nprint(sr)\n<\/code><\/pre>\n<pre><code class=\"language-bash line-numbers\">0    20210104\n1    20210105\n2    20210106\n3    20210107\n4    20210108\n5    20210111\n6    20210112\n7    20210113\n8    20210114\n9    20210115\nName: date, dtype: object\n<\/code><\/pre>\n<p>\u4ece\u6253\u5370\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0c\u6240\u6709\u7684\u65e5\u671f\u90fd\u6309\u7167\u9884\u671f\u8fdb\u884c\u4e86\u4fee\u6539\u3002<\/p>\n<p>\u5bf9\u4e8e\u5e26\u591a\u4e2a\u53c2\u6570\u7684\u51fd\u6570\uff0c\u540c\u6837\u53ef\u4ee5\u4f7f\u7528apply\u65b9\u6cd5\u5e94\u7528\u5230Series\u7684\u6240\u6709\u5143\u7d20\u4e0a\u3002\u6211\u4eec\u5bf9str_replace\u51fd\u6570\u8fdb\u884c\u6539\u9020\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">def str_replace_v2(s, c=''):\n    return s.replace('-', c)\n<\/code><\/pre>\n<p>\u8be5\u51fd\u6570\u9ed8\u8ba4\u4f7f\u7528\u7a7a\u4e32\u6765\u66ff\u6362&#8221;-&#8220;\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6307\u5b9a\u7684\u5b57\u7b26\u4e32c\u6765\u66ff\u6362\u3002\u5728apply\u4e2d\u4f7f\u7528\u65f6\uff0c\u53ea\u9700\u8981\u6307\u5b9ac\u7684\u503c\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">sr = df['date'].apply(str_replace_v2, c='\/')\nprint(sr)\n<\/code><\/pre>\n<pre><code class=\"language-bash line-numbers\">0    2021\/01\/04\n1    2021\/01\/05\n2    2021\/01\/06\n3    2021\/01\/07\n4    2021\/01\/08\n5    2021\/01\/11\n6    2021\/01\/12\n7    2021\/01\/13\n8    2021\/01\/14\n9    2021\/01\/15\nName: date, dtype: object\n<\/code><\/pre>\n<p>\u53e6\uff1a\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528Series\u7684str\u65b9\u6cd5\uff0c\u800c\u4e0d\u9700\u8981\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u6765\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">sr = df['date'].str.replace('-', '')\nprint(sr)\n<\/code><\/pre>\n<p>\u5bf9\u4e8e\u5217Series\u8fdb\u884c\u7684\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u8ba1\u7b97\u7ed3\u679c\u4fdd\u5b58\u4e3adf\u7684\u65b0\u5217\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">df['new_date'] = df['date'].apply(str_replace_v2, c='\/')\nprint(df)\n<\/code><\/pre>\n<pre><code class=\"language-bash line-numbers\">         date   open   high    low  ...     psTTM   pcfNcfTTM  isST    new_date\n0  2021-01-04  20.03  21.40  19.93  ...  3.874122 -336.943766     0  2021\/01\/04\n1  2021-01-05  20.51  21.31  19.95  ...  3.864778 -336.131070     0  2021\/01\/05\n2  2021-01-06  20.83  21.28  19.06  ...  3.694713 -321.340003     0  2021\/01\/06\n3  2021-01-07  19.55  20.44  19.21  ...  3.717139 -323.290473     0  2021\/01\/07\n4  2021-01-08  19.89  20.13  18.60  ...  3.642385 -316.788905     0  2021\/01\/08\n5  2021-01-11  18.90  18.90  17.54  ...  3.279829 -285.256300     0  2021\/01\/11\n6  2021-01-12  17.20  18.35  16.83  ...  3.388222 -294.683573     0  2021\/01\/12\n7  2021-01-13  18.20  19.53  18.20  ...  3.505959 -304.923543     0  2021\/01\/13\n8  2021-01-14  18.69  19.62  18.12  ...  3.625566 -315.326052     0  2021\/01\/14\n9  2021-01-15  19.07  19.68  18.13  ...  3.606877 -313.700660     0  2021\/01\/15\n\n[10 rows x 18 columns]\n<\/code><\/pre>\n<p>\u4ece\u6253\u5370\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\uff0cdf\u65b0\u589e\u4e86\u4e00\u5217new_date\uff0c\u4fdd\u5b58\u4e86apply\u7684\u8ba1\u7b97\u7ed3\u679c\u3002<\/p>\n<hr \/>\n<h1>DataFrame\u7684apply\u65b9\u6cd5<\/h1>\n<p>DataFrame\u7684apply\u4e0eSeries\u7684apply\u4f7f\u7528\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u662f\u9700\u8981\u6307\u5b9a\u51fd\u6570\u662f\u6309\u5217\u8fd8\u662f\u6309\u884c\u8fdb\u884c\uff0c\u901a\u8fc7axis\u53c2\u6570\u8fdb\u884c\u8bbe\u7f6e\uff0caxis\u9ed8\u8ba4\u503c\u4e3a0\uff0c\u8868\u793a\u6309\u5217\u5e94\u7528\u6307\u5b9a\u51fd\u6570\uff0caxis\u4e3a1\u65f6\uff0c\u8868\u793a\u6309\u884c\u5e94\u7528\u6307\u5b9a\u51fd\u6570\u3002<\/p>\n<ol>\n<li>\u6309\u5217\u5e94\u7528<\/li>\n<\/ol>\n<p>\u5047\u5982\u6211\u4eec\u60f3\u8ba1\u7b97open\u3001high\u3001low\u3001close\u5217\u7684\u5747\u503c\uff0c\u9996\u5148\u53d6\u51fa\u8fd94\u5217\uff0c\u770b\u4e00\u4e0b\u6570\u636e\u7c7b\u578b\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">print(type(df[['open', 'high', 'low', 'close']]))\n<\/code><\/pre>\n<pre><code class=\"language-bash line-numbers\">&lt;class 'pandas.core.frame.DataFrame'&gt;\n<\/code><\/pre>\n<p>\u53ef\u89c1<code>df[['open', 'high', 'low', 'close']]<\/code>\u7684\u6570\u636e\u7c7b\u578b\u4e3aDataFrame\u3002<\/p>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u5b9a\u4e49\u8ba1\u7b97\u5747\u503c\u7684\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">def mean_value(row):\n    return numpy.mean(row)\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u8c03\u7528numpy\u7684mean\u51fd\u6570\u6765\u8ba1\u7b97\u5747\u503c\u3002\u6700\u540e\u6211\u4eec\u5c31\u53ef\u4ee5\u5229\u7528apply\u65b9\u6cd5\u6765\u8ba1\u7b97\u5404\u5217\u7684\u5747\u503c\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">print(df[['open', 'high', 'low', 'close']].apply(mean_value))\n<\/code><\/pre>\n<pre><code class=\"language-bash line-numbers\">open     19.287\nhigh     20.064\nlow      18.557\nclose    19.370\ndtype: float64\n<\/code><\/pre>\n<p>apply\u65b9\u6cd5\u8fd8\u662f\u8fd4\u56de\u7684Series\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u91cc\u9762\u4fdd\u5b58\u4e86\u5404\u5217\u7684\u5747\u503c\u3002<\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u53ea\u662f\u7528\u4e8e\u6f14\u793aapply\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">print(df[['open', 'high', 'low', 'close']].mean())\n<\/code><\/pre>\n<p>\u5982\u679c\u60f3\u5728\u5e94\u7528\u51fd\u6570\u4e2d\u8bbf\u95ee\u5230\u5177\u4f53\u5143\u7d20\uff0c\u9700\u8981\u7528\u7d22\u5f15\u6765\u8bbf\u95ee\u3002\u6211\u4eec\u7684\u793a\u4f8b\u4e2d\uff0c\u5404\u884c\u7684\u7d22\u5f15\u662f\u4ece0\u52309\uff0c\u5047\u8bbe\u6211\u4eec\u60f3\u8ba1\u7b97\u7b2c0\u30012\u30015\u884c\u7684\u5747\u503c\uff0c\u53ef\u4ee5\u8fd9\u6837\u6539\u9020\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">def mean_value_v2(row):\n    return (row[0] + row[2] + row[5]) \/ 3\n\nprint(df[['open', 'high', 'low', 'close']].apply(mean_value_v2))\n<\/code><\/pre>\n<pre><code class=\"language-bash line-numbers\">open     19.920000\nhigh     20.526667\nlow      18.843333\nclose    19.350000\ndtype: float64\n<\/code><\/pre>\n<p>\u5728\u4ee3\u7801\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u4f7f\u7528row[0]\u3001row[2]\u8fd9\u6837\u7684\u65b9\u5f0f\u6765\u8bbf\u95ee\u5177\u4f53\u7684\u5143\u7d20\u3002<\/p>\n<ol start=\"2\">\n<li>\u6309\u884c\u5e94\u7528<\/li>\n<\/ol>\n<p>\u6309\u884c\u5e94\u7528\u53ef\u4ee5\u5b9e\u73b0DataFrame\u5355\u5217\u6216\u8005\u591a\u5217\u4e4b\u95f4\u7684\u8fd0\u7b97\u3002<\/p>\n<p>\u6211\u4eec\u4ee5\u8ba1\u7b97\u632f\u5e45=(high-low)\/preclose\u4e3a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">def amp(col):\n    return (col['high'] - col['low']) \/ col['preclose']\n\nprint(df.apply(amp, axis=1))\n<\/code><\/pre>\n<pre><code class=\"language-bash line-numbers\">0    0.073390\n1    0.065605\n2    0.107350\n3    0.062215\n4    0.076923\n5    0.069779\n6    0.086610\n7    0.073359\n8    0.079957\n9    0.079897\ndtype: float64\n<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4e24\u79cdlambda\u8868\u8fbe\u5f0f\u6765\u5b9e\u73b0\u5747\u53ef\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">print(df.apply(lambda x: (x.high - x.low) \/ x.preclose, axis=1))\nprint(df.apply(lambda x: (x['high'] - x['low']) \/ x['preclose'], axis=1))\n<\/code><\/pre>\n<p>\u5982\u679c\u60f3\u628a\u8ba1\u7b97\u7ed3\u679c\u4fdd\u5b58\u5728df\u7684\u5217\u4e2d\uff0c\u53ef\u4ee5\u5982\u4e0b\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">df['amp'] = df.apply(lambda x: (x.high - x.low) \/ x.preclose, axis=1)\nprint(df)\n<\/code><\/pre>\n<pre><code class=\"language-bash line-numbers\">         date   open   high    low  ...     psTTM   pcfNcfTTM  isST       amp\n0  2021-01-04  20.03  21.40  19.93  ...  3.874122 -336.943766     0  0.073390\n1  2021-01-05  20.51  21.31  19.95  ...  3.864778 -336.131070     0  0.065605\n2  2021-01-06  20.83  21.28  19.06  ...  3.694713 -321.340003     0  0.107350\n3  2021-01-07  19.55  20.44  19.21  ...  3.717139 -323.290473     0  0.062215\n4  2021-01-08  19.89  20.13  18.60  ...  3.642385 -316.788905     0  0.076923\n5  2021-01-11  18.90  18.90  17.54  ...  3.279829 -285.256300     0  0.069779\n6  2021-01-12  17.20  18.35  16.83  ...  3.388222 -294.683573     0  0.086610\n7  2021-01-13  18.20  19.53  18.20  ...  3.505959 -304.923543     0  0.073359\n8  2021-01-14  18.69  19.62  18.12  ...  3.625566 -315.326052     0  0.079957\n9  2021-01-15  19.07  19.68  18.13  ...  3.606877 -313.700660     0  0.079897\n\n[10 rows x 18 columns]\n<\/code><\/pre>\n<style>\npre{\n    white-space: pre;\n    overflow-x: auto;\n    overflow-y: auto;\n    max-height: 300px;\n}\n.table-wrapper {\n    overflow-x: auto;\n    overflow-y: auto;\n    white-space: nowrap;\n    max-height: 300px;\n}\n<\/style>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5904\u7406\u80a1\u7968\u6570\u636e\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u628a\u4e00\u4e9b\u51fd\u6570\u5e94\u7528\u4e8e\u591a\u884c\u6216\u8005\u591a\u5217\u6570\u636e\u4e0a\uff0c\u8fd9\u65f6\u5c31\u9700\u8981\u4f7f\u7528Series\u548cDataFrame\u7684 &#8230; <a title=\"Pandas\u7684apply\u65b9\u6cd5\" class=\"read-more\" href=\"\/?p=204\" aria-label=\"\u7ee7\u7eed\u9605\u8bfbPandas\u7684apply\u65b9\u6cd5\">\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":[5],"tags":[],"class_list":["post-204","post","type-post","status-publish","format-standard","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Pandas\u7684apply\u65b9\u6cd5 - Python\u91cf\u5316\u6280\u672f\u5206\u4eab<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/coderx.com.cn\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Pandas\u7684apply\u65b9\u6cd5 - Python\u91cf\u5316\u6280\u672f\u5206\u4eab\" \/>\n<meta property=\"og:description\" content=\"\u5728\u5904\u7406\u80a1\u7968\u6570\u636e\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u628a\u4e00\u4e9b\u51fd\u6570\u5e94\u7528\u4e8e\u591a\u884c\u6216\u8005\u591a\u5217\u6570\u636e\u4e0a\uff0c\u8fd9\u65f6\u5c31\u9700\u8981\u4f7f\u7528Series\u548cDataFrame\u7684 ... \u9605\u8bfb\u66f4\u591a\" \/>\n<meta property=\"og:url\" content=\"\/?p=204\" \/>\n<meta property=\"og:site_name\" content=\"Python\u91cf\u5316\u6280\u672f\u5206\u4eab\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-15T14:34:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-19T13:13:42+00:00\" \/>\n<meta name=\"author\" content=\"mnj\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"mnj\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/coderx.com.cn\/#article\",\"isPartOf\":{\"@id\":\"\/?p=204\"},\"author\":{\"name\":\"mnj\",\"@id\":\"http:\/\/coderx.com.cn\/#\/schema\/person\/a6c1fee270f0514e289f697c4a529ee9\"},\"headline\":\"Pandas\u7684apply\u65b9\u6cd5\",\"datePublished\":\"2021-09-15T14:34:15+00:00\",\"dateModified\":\"2021-09-19T13:13:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"\/?p=204\"},\"wordCount\":136,\"commentCount\":2,\"publisher\":{\"@id\":\"http:\/\/coderx.com.cn\/#\/schema\/person\/a6c1fee270f0514e289f697c4a529ee9\"},\"articleSection\":[\"python\u5de5\u5177\u7bb1\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/coderx.com.cn\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"\/?p=204\",\"url\":\"http:\/\/coderx.com.cn\/\",\"name\":\"Pandas\u7684apply\u65b9\u6cd5 - Python\u91cf\u5316\u6280\u672f\u5206\u4eab\",\"isPartOf\":{\"@id\":\"http:\/\/coderx.com.cn\/#website\"},\"datePublished\":\"2021-09-15T14:34:15+00:00\",\"dateModified\":\"2021-09-19T13:13:42+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/coderx.com.cn\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/coderx.com.cn\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/coderx.com.cn\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"http:\/\/coderx.com.cn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Pandas\u7684apply\u65b9\u6cd5\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/coderx.com.cn\/#website\",\"url\":\"http:\/\/coderx.com.cn\/\",\"name\":\"Python\u91cf\u5316\u6280\u672f\u5206\u4eab-\u7801\u519c\u7532\u7684\u5b66\u4e60\u7b14\u8bb0\",\"description\":\"\u7801\u519c\u7532\u7684\u5b66\u4e60\u7b14\u8bb0\",\"publisher\":{\"@id\":\"http:\/\/coderx.com.cn\/#\/schema\/person\/a6c1fee270f0514e289f697c4a529ee9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/coderx.com.cn\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/coderx.com.cn\/#\/schema\/person\/a6c1fee270f0514e289f697c4a529ee9\",\"name\":\"mnj\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"http:\/\/coderx.com.cn\/#\/schema\/person\/image\/\",\"url\":\"\/wordpress\/wp-content\/uploads\/2021\/09\/cropped-1-2.jpg\",\"contentUrl\":\"\/wordpress\/wp-content\/uploads\/2021\/09\/cropped-1-2.jpg\",\"width\":951,\"height\":189,\"caption\":\"mnj\"},\"logo\":{\"@id\":\"http:\/\/coderx.com.cn\/#\/schema\/person\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Pandas\u7684apply\u65b9\u6cd5 - Python\u91cf\u5316\u6280\u672f\u5206\u4eab","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/coderx.com.cn\/","og_locale":"zh_CN","og_type":"article","og_title":"Pandas\u7684apply\u65b9\u6cd5 - Python\u91cf\u5316\u6280\u672f\u5206\u4eab","og_description":"\u5728\u5904\u7406\u80a1\u7968\u6570\u636e\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u628a\u4e00\u4e9b\u51fd\u6570\u5e94\u7528\u4e8e\u591a\u884c\u6216\u8005\u591a\u5217\u6570\u636e\u4e0a\uff0c\u8fd9\u65f6\u5c31\u9700\u8981\u4f7f\u7528Series\u548cDataFrame\u7684 ... \u9605\u8bfb\u66f4\u591a","og_url":"\/?p=204","og_site_name":"Python\u91cf\u5316\u6280\u672f\u5206\u4eab","article_published_time":"2021-09-15T14:34:15+00:00","article_modified_time":"2021-09-19T13:13:42+00:00","author":"mnj","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"mnj","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/coderx.com.cn\/#article","isPartOf":{"@id":"\/?p=204"},"author":{"name":"mnj","@id":"http:\/\/coderx.com.cn\/#\/schema\/person\/a6c1fee270f0514e289f697c4a529ee9"},"headline":"Pandas\u7684apply\u65b9\u6cd5","datePublished":"2021-09-15T14:34:15+00:00","dateModified":"2021-09-19T13:13:42+00:00","mainEntityOfPage":{"@id":"\/?p=204"},"wordCount":136,"commentCount":2,"publisher":{"@id":"http:\/\/coderx.com.cn\/#\/schema\/person\/a6c1fee270f0514e289f697c4a529ee9"},"articleSection":["python\u5de5\u5177\u7bb1"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/coderx.com.cn\/#respond"]}]},{"@type":"WebPage","@id":"\/?p=204","url":"http:\/\/coderx.com.cn\/","name":"Pandas\u7684apply\u65b9\u6cd5 - Python\u91cf\u5316\u6280\u672f\u5206\u4eab","isPartOf":{"@id":"http:\/\/coderx.com.cn\/#website"},"datePublished":"2021-09-15T14:34:15+00:00","dateModified":"2021-09-19T13:13:42+00:00","breadcrumb":{"@id":"http:\/\/coderx.com.cn\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/coderx.com.cn\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/coderx.com.cn\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/coderx.com.cn\/"},{"@type":"ListItem","position":2,"name":"Pandas\u7684apply\u65b9\u6cd5"}]},{"@type":"WebSite","@id":"http:\/\/coderx.com.cn\/#website","url":"http:\/\/coderx.com.cn\/","name":"Python\u91cf\u5316\u6280\u672f\u5206\u4eab-\u7801\u519c\u7532\u7684\u5b66\u4e60\u7b14\u8bb0","description":"\u7801\u519c\u7532\u7684\u5b66\u4e60\u7b14\u8bb0","publisher":{"@id":"http:\/\/coderx.com.cn\/#\/schema\/person\/a6c1fee270f0514e289f697c4a529ee9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/coderx.com.cn\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"http:\/\/coderx.com.cn\/#\/schema\/person\/a6c1fee270f0514e289f697c4a529ee9","name":"mnj","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"http:\/\/coderx.com.cn\/#\/schema\/person\/image\/","url":"\/wordpress\/wp-content\/uploads\/2021\/09\/cropped-1-2.jpg","contentUrl":"\/wordpress\/wp-content\/uploads\/2021\/09\/cropped-1-2.jpg","width":951,"height":189,"caption":"mnj"},"logo":{"@id":"http:\/\/coderx.com.cn\/#\/schema\/person\/image\/"}}]}},"_links":{"self":[{"href":"http:\/\/coderx.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/coderx.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/coderx.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/coderx.com.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/coderx.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=204"}],"version-history":[{"count":9,"href":"http:\/\/coderx.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/204\/revisions"}],"predecessor-version":[{"id":236,"href":"http:\/\/coderx.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/204\/revisions\/236"}],"wp:attachment":[{"href":"http:\/\/coderx.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/coderx.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/coderx.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}