{"id":1155230,"date":"2025-01-13T18:00:47","date_gmt":"2025-01-13T10:00:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1155230.html"},"modified":"2025-01-13T18:00:50","modified_gmt":"2025-01-13T10:00:50","slug":"python%e7%9a%84pandas%e5%a6%82%e4%bd%95%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1155230.html","title":{"rendered":"python\u7684pandas\u5982\u4f55\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25193914\/6866b3a3-5c4c-43e2-b1a9-dc765126799a.webp\" alt=\"python\u7684pandas\u5982\u4f55\u7528\" \/><\/p>\n<p><p> <strong>Pandas\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\u3002\u4f7f\u7528pandas\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3001\u6570\u636e\u64cd\u4f5c\u3001\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u53ef\u89c6\u5316\u7b49\u3002\u8981\u4f7f\u7528pandas\uff0c\u4f60\u9700\u8981\u638c\u63e1\u4ee5\u4e0b\u51e0\u4e2a\u57fa\u672c\u64cd\u4f5c\uff1a\u5bfc\u5165\u6570\u636e\u3001\u6570\u636e\u9009\u62e9\u548c\u8fc7\u6ee4\u3001\u6570\u636e\u5904\u7406\u548c\u64cd\u4f5c\u3001\u6570\u636e\u53ef\u89c6\u5316\u3002<\/strong>\u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5b9e\u7528\u7684\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165Pandas\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Pandas\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Pandas\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165Pandas\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6570\u636e\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>Pandas\u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\u7684\u5bfc\u5165\uff0c\u5305\u62ecCSV\u3001Excel\u3001SQL\u6570\u636e\u5e93\u3001JSON\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u6570\u636e\u5bfc\u5165\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5bfc\u5165CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>CSV\u6587\u4ef6\u662f\u6700\u5e38\u89c1\u7684\u6570\u636e\u683c\u5f0f\u4e4b\u4e00\uff0c\u53ef\u4ee5\u901a\u8fc7<code>read_csv<\/code>\u51fd\u6570\u6765\u5bfc\u5165CSV\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5bfc\u5165Excel\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>read_excel<\/code>\u51fd\u6570\u6765\u5bfc\u5165Excel\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_excel(&#39;data.xlsx&#39;, sheet_name=&#39;Sheet1&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5bfc\u5165SQL\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>read_sql<\/code>\u51fd\u6570\u6765\u5bfc\u5165SQL\u6570\u636e\u5e93\u7684\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>conn = sqlite3.connect(&#39;database.db&#39;)<\/p>\n<p>df = pd.read_sql(&#39;SELECT * FROM table_name&#39;, conn)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u5bfc\u5165JSON\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>read_json<\/code>\u51fd\u6570\u6765\u5bfc\u5165JSON\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_json(&#39;data.json&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6570\u636e\u9009\u62e9\u548c\u8fc7\u6ee4<\/h3>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u9009\u62e9\u548c\u8fc7\u6ee4\u6570\u636e\uff0c\u5305\u62ec\u6309\u5217\u9009\u62e9\u3001\u6309\u884c\u9009\u62e9\u3001\u6761\u4ef6\u9009\u62e9\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6309\u5217\u9009\u62e9<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5217\u540d\u6765\u9009\u62e9\u6570\u636e\u6846\u4e2d\u7684\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9009\u62e9\u5355\u5217<\/p>\n<p>df[&#39;column_name&#39;]<\/p>\n<h2><strong>\u9009\u62e9\u591a\u5217<\/strong><\/h2>\n<p>df[[&#39;column1&#39;, &#39;column2&#39;]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6309\u884c\u9009\u62e9<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u884c\u7d22\u5f15\u6765\u9009\u62e9\u6570\u636e\u6846\u4e2d\u7684\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9009\u62e9\u5355\u884c<\/p>\n<p>df.loc[0]<\/p>\n<h2><strong>\u9009\u62e9\u591a\u884c<\/strong><\/h2>\n<p>df.loc[0:5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6761\u4ef6\u9009\u62e9<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u6765\u9009\u62e9\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9009\u62e9\u6ee1\u8db3\u6761\u4ef6\u7684\u884c<\/p>\n<p>df[df[&#39;column_name&#39;] &gt; value]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6570\u636e\u5904\u7406\u548c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u5904\u7406\u548c\u64cd\u4f5c\u529f\u80fd\uff0c\u5305\u62ec\u6570\u636e\u6e05\u6d17\u3001\u6570\u636e\u8f6c\u6362\u3001\u6570\u636e\u5408\u5e76\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u6e05\u6d17<\/h4>\n<\/p>\n<p><p>\u6570\u636e\u6e05\u6d17\u662f\u6570\u636e\u5206\u6790\u7684\u91cd\u8981\u6b65\u9aa4\uff0cPandas\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u636e\u6e05\u6d17\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u68c0\u67e5\u7a7a\u503c<\/p>\n<p>df.isnull().sum()<\/p>\n<h2><strong>\u5220\u9664\u7a7a\u503c<\/strong><\/h2>\n<p>df.dropna()<\/p>\n<h2><strong>\u586b\u5145\u7a7a\u503c<\/strong><\/h2>\n<p>df.fillna(value)<\/p>\n<h2><strong>\u5220\u9664\u91cd\u590d\u503c<\/strong><\/h2>\n<p>df.drop_duplicates()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u6570\u636e\u8f6c\u6362\u5305\u62ec\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u3001\u6570\u636e\u683c\u5f0f\u8f6c\u6362\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6570\u636e\u7c7b\u578b\u8f6c\u6362<\/p>\n<p>df[&#39;column_name&#39;] = df[&#39;column_name&#39;].astype(&#39;int&#39;)<\/p>\n<h2><strong>\u6570\u636e\u683c\u5f0f\u8f6c\u6362<\/strong><\/h2>\n<p>df[&#39;date&#39;] = pd.to_datetime(df[&#39;date&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6570\u636e\u5408\u5e76<\/h4>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u636e\u5408\u5e76\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u8fde\u63a5\u3001\u5408\u5e76\u3001\u62fc\u63a5\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8fde\u63a5\u6570\u636e\u6846<\/p>\n<p>df1.append(df2)<\/p>\n<h2><strong>\u5408\u5e76\u6570\u636e\u6846<\/strong><\/h2>\n<p>pd.merge(df1, df2, on=&#39;key&#39;)<\/p>\n<h2><strong>\u62fc\u63a5\u6570\u636e\u6846<\/strong><\/h2>\n<p>pd.concat([df1, df2], axis=0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6570\u636e\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>Pandas\u4e0eMatplotlib\u3001Seaborn\u7b49\u53ef\u89c6\u5316\u5e93\u96c6\u6210\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>df.plot(kind=&#39;line&#39;, x=&#39;x_column&#39;, y=&#39;y_column&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u7ed8\u5236\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>df.plot(kind=&#39;bar&#39;, x=&#39;x_column&#39;, y=&#39;y_column&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>df.plot(kind=&#39;scatter&#39;, x=&#39;x_column&#39;, y=&#39;y_column&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6df1\u5165\u6570\u636e\u5904\u7406\u548c\u5206\u6790<\/h3>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u5206\u7ec4\u548c\u805a\u5408<\/h4>\n<\/p>\n<p><p>\u6570\u636e\u5206\u7ec4\u548c\u805a\u5408\u662f\u6570\u636e\u5206\u6790\u4e2d\u5e38\u7528\u7684\u64cd\u4f5c\uff0c\u7528\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u7edf\u8ba1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u5217\u5206\u7ec4<\/p>\n<p>grouped = df.groupby(&#39;column_name&#39;)<\/p>\n<h2><strong>\u8ba1\u7b97\u5206\u7ec4\u540e\u7684\u5747\u503c<\/strong><\/h2>\n<p>grouped.mean()<\/p>\n<h2><strong>\u8ba1\u7b97\u5206\u7ec4\u540e\u7684\u603b\u548c<\/strong><\/h2>\n<p>grouped.sum()<\/p>\n<h2><strong>\u8ba1\u7b97\u5206\u7ec4\u540e\u7684\u8ba1\u6570<\/strong><\/h2>\n<p>grouped.size()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u900f\u89c6\u8868<\/h4>\n<\/p>\n<p><p>\u6570\u636e\u900f\u89c6\u8868\u662f\u6570\u636e\u5206\u6790\u4e2d\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u591a\u7ef4\u5ea6\u7684\u6570\u636e\u6c47\u603b\u548c\u5206\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u6570\u636e\u900f\u89c6\u8868<\/p>\n<p>pivot_table = df.pivot_table(values=&#39;value_column&#39;, index=&#39;index_column&#39;, columns=&#39;columns_column&#39;, aggfunc=&#39;mean&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u65f6\u95f4\u5e8f\u5217\u5206\u6790<\/h4>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u7684\u5904\u7406\u548c\u5206\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u65f6\u95f4\u5e8f\u5217\u7d22\u5f15<\/p>\n<p>df.set_index(&#39;date&#39;, inplace=True)<\/p>\n<h2><strong>\u91cd\u91c7\u6837<\/strong><\/h2>\n<p>df.resample(&#39;M&#39;).mean()<\/p>\n<h2><strong>\u6eda\u52a8\u8ba1\u7b97<\/strong><\/h2>\n<p>df.rolling(window=3).mean()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6570\u636e\u5bfc\u51fa<\/h3>\n<\/p>\n<p><p>Pandas\u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\u7684\u5bfc\u51fa\uff0c\u5305\u62ecCSV\u3001Excel\u3001SQL\u6570\u636e\u5e93\u3001JSON\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u6570\u636e\u5bfc\u51fa\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5bfc\u51faCSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>to_csv<\/code>\u51fd\u6570\u6765\u5bfc\u51fa\u6570\u636e\u5230CSV\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df.to_csv(&#39;output.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5bfc\u51faExcel\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>to_excel<\/code>\u51fd\u6570\u6765\u5bfc\u51fa\u6570\u636e\u5230Excel\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df.to_excel(&#39;output.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5bfc\u51faSQL\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>to_sql<\/code>\u51fd\u6570\u6765\u5bfc\u51fa\u6570\u636e\u5230SQL\u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>conn = sqlite3.connect(&#39;database.db&#39;)<\/p>\n<p>df.to_sql(&#39;table_name&#39;, conn, if_exists=&#39;replace&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u5bfc\u51faJSON\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>to_json<\/code>\u51fd\u6570\u6765\u5bfc\u51fa\u6570\u636e\u5230JSON\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df.to_json(&#39;output.json&#39;, orient=&#39;records&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u8fdb\u9636\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u5927\u6570\u636e<\/h4>\n<\/p>\n<p><p>Pandas\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u6027\u80fd\u95ee\u9898\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u6765\u4f18\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5206\u5757\u8bfb\u53d6\u6570\u636e<\/p>\n<p>for chunk in pd.read_csv(&#39;large_data.csv&#39;, chunksize=10000):<\/p>\n<p>    # \u5904\u7406\u6bcf\u4e2a\u5206\u5757\u6570\u636e<\/p>\n<p>    process(chunk)<\/p>\n<h2><strong>\u4f7f\u7528Dask\u5e93<\/strong><\/h2>\n<p>import dask.dataframe as dd<\/p>\n<p>df = dd.read_csv(&#39;large_data.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u53ef\u89c6\u5316\u8fdb\u9636<\/h4>\n<\/p>\n<p><p>Pandas\u4e0eSeaborn\u7b49\u53ef\u89c6\u5316\u5e93\u96c6\u6210\uff0c\u53ef\u4ee5\u521b\u5efa\u66f4\u9ad8\u7ea7\u7684\u6570\u636e\u53ef\u89c6\u5316\u56fe\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<h2><strong>\u7ed8\u5236\u70ed\u529b\u56fe<\/strong><\/h2>\n<p>sns.heatmap(df.corr(), annot=True)<\/p>\n<h2><strong>\u7ed8\u5236\u7bb1\u7ebf\u56fe<\/strong><\/h2>\n<p>sns.boxplot(x=&#39;x_column&#39;, y=&#39;y_column&#39;, data=df)<\/p>\n<h2><strong>\u7ed8\u5236\u5206\u5e03\u56fe<\/strong><\/h2>\n<p>sns.distplot(df[&#39;column_name&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\u5e94\u7528<\/h4>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86<code>apply<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u81ea\u5b9a\u4e49\u51fd\u6570\u5e94\u7528\u5230\u6570\u636e\u6846\u7684\u884c\u6216\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u81ea\u5b9a\u4e49\u51fd\u6570<\/p>\n<p>def custom_function(x):<\/p>\n<p>    return x * 2<\/p>\n<h2><strong>\u5e94\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u5230\u5217<\/strong><\/h2>\n<p>df[&#39;new_column&#39;] = df[&#39;column_name&#39;].apply(custom_function)<\/p>\n<h2><strong>\u5e94\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u5230\u884c<\/strong><\/h2>\n<p>df[&#39;new_column&#39;] = df.apply(lambda row: custom_function(row[&#39;column_name&#39;]), axis=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5b9e\u6218\u6848\u4f8b<\/h3>\n<\/p>\n<p><h4>1\u3001\u80a1\u7968\u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Pandas\u6765\u5206\u6790\u80a1\u7968\u6570\u636e\uff0c\u5305\u62ec\u6570\u636e\u5bfc\u5165\u3001\u6570\u636e\u5904\u7406\u3001\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u53ef\u89c6\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u5bfc\u5165\u6570\u636e<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;stock_data.csv&#39;)<\/p>\n<h2><strong>\u6570\u636e\u5904\u7406<\/strong><\/h2>\n<p>df[&#39;Date&#39;] = pd.to_datetime(df[&#39;Date&#39;])<\/p>\n<p>df.set_index(&#39;Date&#39;, inplace=True)<\/p>\n<h2><strong>\u6570\u636e\u5206\u6790<\/strong><\/h2>\n<p>df[&#39;D<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ly Return&#39;] = df[&#39;Close&#39;].pct_change()<\/p>\n<p>df[&#39;Cumulative Return&#39;] = (1 + df[&#39;Daily Return&#39;]).cumprod()<\/p>\n<h2><strong>\u6570\u636e\u53ef\u89c6\u5316<\/strong><\/h2>\n<p>df[&#39;Close&#39;].plot(title=&#39;Stock Price&#39;)<\/p>\n<p>plt.show()<\/p>\n<p>df[&#39;Daily Return&#39;].plot(title=&#39;Daily Return&#39;)<\/p>\n<p>plt.show()<\/p>\n<p>df[&#39;Cumulative Return&#39;].plot(title=&#39;Cumulative Return&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5ba2\u6237\u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Pandas\u6765\u5206\u6790\u5ba2\u6237\u6570\u636e\uff0c\u5305\u62ec\u6570\u636e\u5bfc\u5165\u3001\u6570\u636e\u6e05\u6d17\u3001\u6570\u636e\u5206\u7ec4\u548c\u805a\u5408\u3001\u6570\u636e\u53ef\u89c6\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u5bfc\u5165\u6570\u636e<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;customer_data.csv&#39;)<\/p>\n<h2><strong>\u6570\u636e\u6e05\u6d17<\/strong><\/h2>\n<p>df.dropna(inplace=True)<\/p>\n<p>df[&#39;Age&#39;] = df[&#39;Age&#39;].astype(&#39;int&#39;)<\/p>\n<h2><strong>\u6570\u636e\u5206\u7ec4\u548c\u805a\u5408<\/strong><\/h2>\n<p>age_group = df.groupby(&#39;Age&#39;).size()<\/p>\n<h2><strong>\u6570\u636e\u53ef\u89c6\u5316<\/strong><\/h2>\n<p>age_group.plot(kind=&#39;bar&#39;, title=&#39;Customer Age Distribution&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\uff0c\u638c\u63e1Pandas\u7684\u57fa\u672c\u64cd\u4f5c\u548c\u9ad8\u7ea7\u529f\u80fd\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u6570\u636e\u5206\u6790\u7684\u6548\u7387\u548c\u6548\u679c\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684Pandas\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u5176\u4ed6Python\u5e93\u5982Matplotlib\u3001Seaborn\u7b49\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\uff0c\u8fdb\u4e00\u6b65\u63d0\u5347\u6570\u636e\u5206\u6790\u7684\u6548\u679c\u3002\u901a\u8fc7\u4e0d\u65ad\u7684\u5b9e\u8df5\u548c\u5b66\u4e60\uff0c\u53ef\u4ee5\u6df1\u5165\u638c\u63e1Pandas\u7684\u5404\u79cd\u529f\u80fd\uff0c\u6210\u4e3a\u6570\u636e\u5206\u6790\u9886\u57df\u7684\u4e13\u5bb6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5b89\u88c5Pandas\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u4f7f\u7528Pandas\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5Pandas\uff1a  <\/p>\n<pre><code class=\"language-bash\">pip install pandas\n<\/code><\/pre>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728Python\u811a\u672c\u6216\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\u5bfc\u5165Pandas\uff0c\u901a\u8fc7<code>import pandas as pd<\/code>\u6765\u4f7f\u7528\u3002<\/p>\n<p><strong>Pandas\u5728\u6570\u636e\u5206\u6790\u4e2d\u6709\u54ea\u4e9b\u4e3b\u8981\u529f\u80fd\uff1f<\/strong><br \/>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5de5\u5177\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u529f\u80fd\u3002\u5b83\u80fd\u591f\u65b9\u4fbf\u5730\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u96c6\uff0c\u5305\u62ec\u6570\u636e\u6e05\u6d17\u3001\u7b5b\u9009\u3001\u5206\u7ec4\u3001\u5408\u5e76\u3001\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u7b49\u3002Pandas\u8fd8\u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\u7684\u8bfb\u53d6\u548c\u5199\u5165\uff0c\u5982CSV\u3001Excel\u3001SQL\u6570\u636e\u5e93\u7b49\uff0c\u4f7f\u5f97\u6570\u636e\u7684\u5904\u7406\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Pandas\u8bfb\u53d6\u548c\u5904\u7406CSV\u6587\u4ef6\uff1f<\/strong><br \/>\u4f7f\u7528Pandas\u8bfb\u53d6CSV\u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528<code>pd.read_csv()<\/code>\u51fd\u6570\u6765\u52a0\u8f7d\u6570\u636e\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\ndata = pd.read_csv(&#39;file.csv&#39;)\n<\/code><\/pre>\n<p>\u8bfb\u53d6\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Pandas\u63d0\u4f9b\u7684\u5404\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u6570\u636e\uff0c\u5982<code>data.head()<\/code>\u67e5\u770b\u524d\u51e0\u884c\u6570\u636e\uff0c<code>data.describe()<\/code>\u83b7\u53d6\u6570\u636e\u7684\u7edf\u8ba1\u4fe1\u606f\uff0c\u6216\u8005\u901a\u8fc7\u6761\u4ef6\u7b5b\u9009\u6765\u63d0\u53d6\u7279\u5b9a\u7684\u6570\u636e\u884c\u3002<\/p>\n<p><strong>\u5728Pandas\u4e2d\u5982\u4f55\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\uff1f<\/strong><br \/>\u867d\u7136Pandas\u672c\u8eab\u4e0d\u63d0\u4f9b\u6570\u636e\u53ef\u89c6\u5316\u529f\u80fd\uff0c\u4f46\u5b83\u53ef\u4ee5\u4e0eMatplotlib\u6216Seaborn\u7b49\u5e93\u914d\u5408\u4f7f\u7528\uff0c\u4ece\u800c\u5b9e\u73b0\u6570\u636e\u7684\u53ef\u89c6\u5316\u3002\u53ef\u4ee5\u901a\u8fc7<code>data.plot()<\/code>\u65b9\u6cd5\u5feb\u901f\u751f\u6210\u56fe\u8868\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528Seaborn\u7684\u7ed8\u56fe\u529f\u80fd\u6765\u521b\u5efa\u66f4\u4e3a\u590d\u6742\u7684\u53ef\u89c6\u5316\u6548\u679c\u3002\u901a\u8fc7\u8bbe\u7f6e\u9002\u5f53\u7684\u53c2\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9a\u5236\u56fe\u8868\u7684\u6837\u5f0f\u548c\u5916\u89c2\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Pandas\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\u3002\u4f7f\u7528pandas\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3001\u6570\u636e\u64cd\u4f5c\u3001\u6570\u636e\u5206 [&hellip;]","protected":false},"author":3,"featured_media":1155236,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155230"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1155230"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155230\/revisions"}],"predecessor-version":[{"id":1155237,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155230\/revisions\/1155237"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1155236"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1155230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1155230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1155230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}