{"id":1160486,"date":"2025-01-13T19:05:24","date_gmt":"2025-01-13T11:05:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1160486.html"},"modified":"2025-01-13T19:05:27","modified_gmt":"2025-01-13T11:05:27","slug":"%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e5%a4%a7%e9%87%8f%e6%95%b0%e6%8d%ae%e8%bf%9b%e5%85%a5python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1160486.html","title":{"rendered":"\u5982\u4f55\u8f93\u5165\u5927\u91cf\u6570\u636e\u8fdb\u5165python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25201939\/00d68c40-94c7-40c6-8c13-5dab5950500b.webp\" alt=\"\u5982\u4f55\u8f93\u5165\u5927\u91cf\u6570\u636e\u8fdb\u5165python\" \/><\/p>\n<p><p> <strong>\u8981\u5c06\u5927\u91cf\u6570\u636e\u8f93\u5165\u5230Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5982\u6587\u4ef6\u8bfb\u53d6\u3001\u6570\u636e\u5e93\u8fde\u63a5\u3001API\u8c03\u7528\u7b49<\/strong>\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec<strong>\u4f7f\u7528pandas\u8bfb\u53d6CSV\u6587\u4ef6\u3001\u901a\u8fc7SQLAlchemy\u8fde\u63a5\u6570\u636e\u5e93\u3001\u8c03\u7528RESTful API\u83b7\u53d6\u6570\u636e<\/strong>\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u2014\u2014\u4f7f\u7528pandas\u8bfb\u53d6CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528pandas\u8bfb\u53d6CSV\u6587\u4ef6<\/strong>\uff1apandas\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u8bfb\u53d6\u548c\u5904\u7406\u6570\u636e\u7684\u65b9\u6cd5\u3002\u8bfb\u53d6CSV\u6587\u4ef6\u662f\u6700\u5e38\u89c1\u7684\u6570\u636e\u8f93\u5165\u65b9\u6cd5\u4e4b\u4e00\u3002\u4f7f\u7528pandas\u8bfb\u53d6CSV\u6587\u4ef6\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5pandas\u5e93\uff1a\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c<code>pip install pandas<\/code>\u3002<\/li>\n<li>\u5bfc\u5165pandas\u5e93\uff1a\u5728Python\u811a\u672c\u4e2d\u4f7f\u7528<code>import pandas as pd<\/code>\u5bfc\u5165pandas\u5e93\u3002<\/li>\n<li>\u8bfb\u53d6CSV\u6587\u4ef6\uff1a\u4f7f\u7528<code>pd.read_csv()<\/code>\u51fd\u6570\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5230DataFrame\u4e2d\u3002DataFrame\u662fpandas\u4e2d\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7c7b\u4f3c\u4e8e\u7535\u5b50\u8868\u683c\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u663e\u793a\u524d5\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u8f93\u5165\u5927\u91cf\u6570\u636e\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u6587\u4ef6\u8bfb\u53d6<\/h2>\n<\/p>\n<p><h3>1.1\u3001\u8bfb\u53d6CSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>CSV\uff08Comma Separated Values\uff09\u6587\u4ef6\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u5b58\u50a8\u683c\u5f0f\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002\u4f7f\u7528pandas\u5e93\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u548c\u5904\u7406CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5pandas\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165pandas\u5e93\u5e76\u8bfb\u53d6CSV\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u663e\u793a\u524d5\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679cCSV\u6587\u4ef6\u8f83\u5927\uff0c\u53ef\u4ee5\u4f7f\u7528<code>chunksize<\/code>\u53c2\u6570\u5206\u5757\u8bfb\u53d6\u6570\u636e\uff0c\u4ece\u800c\u8282\u7701\u5185\u5b58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">chunksize = 10000<\/p>\n<p>for chunk in pd.read_csv(&#39;data.csv&#39;, chunksize=chunksize):<\/p>\n<p>    # \u5904\u7406\u6bcf\u4e2a\u6570\u636e\u5757<\/p>\n<p>    print(chunk.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.2\u3001\u8bfb\u53d6Excel\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>Excel\u6587\u4ef6\u4e5f\u662f\u5e38\u89c1\u7684\u6570\u636e\u5b58\u50a8\u683c\u5f0f\u3002pandas\u5e93\u63d0\u4f9b\u4e86\u8bfb\u53d6Excel\u6587\u4ef6\u7684\u529f\u80fd\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5openpyxl\u5e93\u4ee5\u652f\u6301Excel\u6587\u4ef6\u8bfb\u53d6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u8bfb\u53d6Excel\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_excel(&#39;data.xlsx&#39;)<\/p>\n<h2><strong>\u663e\u793a\u524d5\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.3\u3001\u8bfb\u53d6JSON\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\u3002\u4f7f\u7528pandas\u5e93\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u548c\u5904\u7406JSON\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\u8bfb\u53d6JSON\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6JSON\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_json(&#39;data.json&#39;)<\/p>\n<h2><strong>\u663e\u793a\u524d5\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u6570\u636e\u5e93\u8fde\u63a5<\/h2>\n<\/p>\n<p><h3>2.1\u3001\u4f7f\u7528SQLAlchemy\u8fde\u63a5\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>SQLAlchemy\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5e93\u5de5\u5177\u5305\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\u7c7b\u578b\uff0c\u5982MySQL\u3001PostgreSQL\u3001SQLite\u7b49\u3002\u4f7f\u7528SQLAlchemy\u53ef\u4ee5\u8f7b\u677e\u8fde\u63a5\u6570\u636e\u5e93\u5e76\u8bfb\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5SQLAlchemy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install SQLAlchemy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u8fde\u63a5\u6570\u636e\u5e93\u5e76\u8bfb\u53d6\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>engine = create_engine(&#39;mysql+pymysql:\/\/username:password@host:port\/database&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e\u8868<\/strong><\/h2>\n<p>data = pd.read_sql(&#39;table_name&#39;, engine)<\/p>\n<h2><strong>\u663e\u793a\u524d5\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.2\u3001\u4f7f\u7528Pandas\u8fde\u63a5\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86SQLAlchemy\uff0cpandas\u5e93\u4e5f\u63d0\u4f9b\u4e86\u76f4\u63a5\u8fde\u63a5\u6570\u636e\u5e93\u7684\u65b9\u6cd5\u3002\u4f7f\u7528<code>pd.read_sql()<\/code>\u51fd\u6570\u53ef\u4ee5\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\u8fde\u63a5\u6570\u636e\u5e93\u5e76\u8bfb\u53d6\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import sqlite3<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;database.db&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e\u8868<\/strong><\/h2>\n<p>data = pd.read_sql(&#39;SELECT * FROM table_name&#39;, conn)<\/p>\n<h2><strong>\u663e\u793a\u524d5\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001API\u8c03\u7528<\/h2>\n<\/p>\n<p><h3>3.1\u3001\u4f7f\u7528Requests\u5e93\u8c03\u7528RESTful API<\/h3>\n<\/p>\n<p><p>Requests\u662fPython\u4e2d\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684HTTP\u5e93\uff0c\u9002\u7528\u4e8e\u8c03\u7528RESTful API\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Requests\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u8c03\u7528RESTful API\u5e76\u8bfb\u53d6\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u8c03\u7528RESTful API<\/strong><\/h2>\n<p>response = requests.get(&#39;https:\/\/api.example.com\/data&#39;)<\/p>\n<h2><strong>\u89e3\u6790JSON\u6570\u636e<\/strong><\/h2>\n<p>data = response.json()<\/p>\n<h2><strong>\u5c06\u6570\u636e\u8f6c\u6362\u4e3aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u663e\u793a\u524d5\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2\u3001\u4f7f\u7528Pandas\u8bfb\u53d6API\u6570\u636e<\/h3>\n<\/p>\n<p><p>pandas\u5e93\u63d0\u4f9b\u4e86\u76f4\u63a5\u8bfb\u53d6API\u6570\u636e\u7684\u65b9\u6cd5\u3002\u4f7f\u7528<code>pd.read_json()<\/code>\u51fd\u6570\u53ef\u4ee5\u4eceAPI\u4e2d\u8bfb\u53d6JSON\u6570\u636e\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aDataFrame\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\u8bfb\u53d6API\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6API\u6570\u636e<\/strong><\/h2>\n<p>url = &#39;https:\/\/api.example.com\/data&#39;<\/p>\n<p>data = pd.read_json(url)<\/p>\n<h2><strong>\u663e\u793a\u524d5\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u6570\u636e\u5904\u7406\u4e0e\u4f18\u5316<\/h2>\n<\/p>\n<p><h3>4.1\u3001\u6570\u636e\u6e05\u6d17<\/h3>\n<\/p>\n<p><p>\u5728\u5c06\u5927\u91cf\u6570\u636e\u8f93\u5165\u5230Python\u4e2d\u540e\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u6d17\u548c\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u8d28\u91cf\u548c\u4e00\u81f4\u6027\u3002\u5e38\u89c1\u7684\u6570\u636e\u6e05\u6d17\u64cd\u4f5c\u5305\u62ec\u5904\u7406\u7f3a\u5931\u503c\u3001\u53bb\u91cd\u3001\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5904\u7406\u7f3a\u5931\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c<\/strong><\/h2>\n<p>data = data.dropna()<\/p>\n<h2><strong>\u586b\u5145\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>data = data.fillna(0)<\/p>\n<h2><strong>\u663e\u793a\u524d5\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.2\u3001\u6570\u636e\u8f6c\u6362\u4e0e\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u8f6c\u6362\u548c\u4f18\u5316\uff0c\u4ee5\u63d0\u9ad8\u6570\u636e\u5904\u7406\u6548\u7387\u548c\u6027\u80fd\u3002\u5e38\u89c1\u7684\u6570\u636e\u8f6c\u6362\u64cd\u4f5c\u5305\u62ec\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u3001\u6570\u636e\u5206\u5757\u5904\u7406\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u5c06\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u6574\u6570<\/strong><\/h2>\n<p>data[&#39;column_name&#39;] = data[&#39;column_name&#39;].astype(int)<\/p>\n<h2><strong>\u663e\u793a\u524d5\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u5e76\u884c\u5904\u7406\u4e0e\u5206\u5e03\u5f0f\u8ba1\u7b97<\/h2>\n<\/p>\n<p><h3>5.1\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u4e0e\u591a\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u4e0e\u591a\u8fdb\u7a0b\u6280\u672f\u6765\u63d0\u9ad8\u6570\u636e\u5904\u7406\u901f\u5ea6\u3002Python\u4e2d\u7684<code>threading<\/code>\u548c<code>multiprocessing<\/code>\u5e93\u63d0\u4f9b\u4e86\u591a\u7ebf\u7a0b\u4e0e\u591a\u8fdb\u7a0b\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u8bfb\u53d6\u591a\u4e2aCSV\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>from threading import Thread<\/p>\n<p>def read_csv(file):<\/p>\n<p>    data = pd.read_csv(file)<\/p>\n<p>    print(data.head())<\/p>\n<p>files = [&#39;data1.csv&#39;, &#39;data2.csv&#39;, &#39;data3.csv&#39;]<\/p>\n<p>threads = []<\/p>\n<p>for file in files:<\/p>\n<p>    thread = Thread(target=read_csv, args=(file,))<\/p>\n<p>    threads.append(thread)<\/p>\n<p>    thread.start()<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.2\u3001\u4f7f\u7528Dask\u5e93\u8fdb\u884c\u5206\u5e03\u5f0f\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>Dask\u662fPython\u4e2d\u4e00\u4e2a\u5e76\u884c\u8ba1\u7b97\u5e93\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002\u4f7f\u7528Dask\u53ef\u4ee5\u8f7b\u677e\u8fdb\u884c\u5206\u5e03\u5f0f\u8ba1\u7b97\uff0c\u5e76\u63d0\u9ad8\u6570\u636e\u5904\u7406\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Dask\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install dask<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u4f7f\u7528Dask\u8bfb\u53d6\u548c\u5904\u7406\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import dask.dataframe as dd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = dd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u663e\u793a\u524d5\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u6570\u636e\u5b58\u50a8\u4e0e\u8f93\u51fa<\/h2>\n<\/p>\n<p><h3>6.1\u3001\u5c06\u6570\u636e\u5b58\u50a8\u5230\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5b8c\u5927\u91cf\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u5230\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u4f7f\u7528\u3002\u5e38\u89c1\u7684\u6570\u636e\u5b58\u50a8\u683c\u5f0f\u5305\u62ecCSV\u3001Excel\u3001JSON\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5c06\u6570\u636e\u5b58\u50a8\u5230CSV\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u5904\u7406\u6570\u636e<\/strong><\/h2>\n<h2><strong>...<\/strong><\/h2>\n<h2><strong>\u5b58\u50a8\u6570\u636e\u5230CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data.to_csv(&#39;output.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6.2\u3001\u5c06\u6570\u636e\u5b58\u50a8\u5230\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5c06\u6570\u636e\u5b58\u50a8\u5230\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d\u3002\u4f7f\u7528SQLAlchemy\u6216pandas\u5e93\u53ef\u4ee5\u8f7b\u677e\u5c06\u6570\u636e\u5b58\u50a8\u5230\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5c06\u6570\u636e\u5b58\u50a8\u5230MySQL\u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>engine = create_engine(&#39;mysql+pymysql:\/\/username:password@host:port\/database&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u5904\u7406\u6570\u636e<\/strong><\/h2>\n<h2><strong>...<\/strong><\/h2>\n<h2><strong>\u5b58\u50a8\u6570\u636e\u5230\u6570\u636e\u5e93<\/strong><\/h2>\n<p>data.to_sql(&#39;table_name&#39;, engine, index=False, if_exists=&#39;replace&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e03\u3001\u6570\u636e\u53ef\u89c6\u5316<\/h2>\n<\/p>\n<p><h3>7.1\u3001\u4f7f\u7528Matplotlib\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u9002\u7528\u4e8e\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u3002\u4f7f\u7528Matplotlib\u53ef\u4ee5\u8f7b\u677e\u5bf9\u5927\u91cf\u6570\u636e\u8fdb\u884c\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Matplotlib\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u8fdb\u884c\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>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>plt.plot(data[&#39;column_x&#39;], data[&#39;column_y&#39;])<\/p>\n<p>plt.xlabel(&#39;X\u8f74\u6807\u7b7e&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74\u6807\u7b7e&#39;)<\/p>\n<p>plt.title(&#39;\u6298\u7ebf\u56fe\u6807\u9898&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>7.2\u3001\u4f7f\u7528Seaborn\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>Seaborn\u662f\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u7f8e\u89c2\u7684\u56fe\u8868\u548c\u66f4\u7b80\u4fbf\u7684\u7ed8\u56fe\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Seaborn\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install seaborn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>sns.scatterplot(x=&#39;column_x&#39;, y=&#39;column_y&#39;, data=data)<\/p>\n<p>plt.xlabel(&#39;X\u8f74\u6807\u7b7e&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74\u6807\u7b7e&#39;)<\/p>\n<p>plt.title(&#39;\u6563\u70b9\u56fe\u6807\u9898&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516b\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5c06\u5927\u91cf\u6570\u636e\u8f93\u5165\u5230Python\u4e2d\uff0c\u5e76\u8fdb\u884c\u6570\u636e\u5904\u7406\u3001\u5b58\u50a8\u548c\u53ef\u89c6\u5316\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec<strong>\u4f7f\u7528pandas\u8bfb\u53d6CSV\u6587\u4ef6\u3001\u901a\u8fc7SQLAlchemy\u8fde\u63a5\u6570\u636e\u5e93\u3001\u8c03\u7528RESTful API\u83b7\u53d6\u6570\u636e<\/strong>\u7b49\u3002\u6570\u636e\u6e05\u6d17\u3001\u8f6c\u6362\u4e0e\u4f18\u5316\u3001\u591a\u7ebf\u7a0b\u4e0e\u591a\u8fdb\u7a0b\u3001\u5206\u5e03\u5f0f\u8ba1\u7b97\u7b49\u6280\u672f\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u5904\u7406\u6548\u7387\u3002\u6700\u540e\uff0c\u53ef\u4ee5\u5c06\u5904\u7406\u540e\u7684\u6570\u636e\u5b58\u50a8\u5230\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\uff0c\u5e76\u4f7f\u7528Matplotlib\u6216Seaborn\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u548c\u5206\u6790\u5927\u91cf\u6570\u636e\uff0c\u63d0\u9ad8\u6570\u636e\u5206\u6790\u7684\u6548\u7387\u548c\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u548c\u8f93\u5165\u5927\u91cf\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5904\u7406\u5927\u91cf\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Pandas\u5e93\u8fdb\u884c\u6570\u636e\u5206\u6790\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\u548c\u64cd\u4f5c\u5de5\u5177\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u96c6\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6CSV\u3001Excel\u6216\u6570\u636e\u5e93\u7b49\u6587\u4ef6\u683c\u5f0f\u6765\u8f93\u5165\u6570\u636e\uff0c\u5229\u7528Pandas\u7684<code>read_csv()<\/code>\u6216<code>read_excel()<\/code>\u65b9\u6cd5\u5b9e\u73b0\u9ad8\u6548\u5bfc\u5165\u3002<\/p>\n<p><strong>Python\u652f\u6301\u54ea\u79cd\u6570\u636e\u683c\u5f0f\u8f93\u5165\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\u8f93\u5165\uff0c\u5305\u62ecCSV\u3001JSON\u3001Excel\u3001SQL\u6570\u636e\u5e93\u3001\u6587\u672c\u6587\u4ef6\u7b49\u3002Pandas\u5e93\u7279\u522b\u64c5\u957f\u5904\u7406\u8fd9\u4e9b\u683c\u5f0f\uff0c\u901a\u8fc7\u4f7f\u7528\u4e0d\u540c\u7684\u8bfb\u53d6\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u52a0\u8f7d\u548c\u5206\u6790\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>pd.read_json()<\/code>\u53ef\u4ee5\u76f4\u63a5\u8bfb\u53d6JSON\u683c\u5f0f\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d0\u9ad8Python\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u7684\u6027\u80fd\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u5347Python\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u4ee5\u4e0b\u7b56\u7565\uff1a\u5229\u7528Pandas\u8fdb\u884c\u6570\u636e\u5904\u7406\u65f6\uff0c\u5c3d\u91cf\u4f7f\u7528\u5411\u91cf\u5316\u64cd\u4f5c\u800c\u975e\u5faa\u73af\uff1b\u5728\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7c7b\u578b\u4ee5\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\uff1b\u4f7f\u7528Dask\u5e93\u8fdb\u884c\u5206\u5e03\u5f0f\u8ba1\u7b97\uff0c\u5904\u7406\u66f4\u5927\u89c4\u6a21\u7684\u6570\u636e\u96c6\u3002\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574Python\u7684\u5185\u5b58\u7ba1\u7406\u6216\u4f7f\u7528NumPy\u5e93\u6765\u52a0\u901f\u6570\u503c\u8ba1\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06\u5927\u91cf\u6570\u636e\u8f93\u5165\u5230Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5982\u6587\u4ef6\u8bfb\u53d6\u3001\u6570\u636e\u5e93\u8fde\u63a5\u3001API\u8c03\u7528\u7b49\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528pa [&hellip;]","protected":false},"author":3,"featured_media":1160495,"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\/1160486"}],"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=1160486"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160486\/revisions"}],"predecessor-version":[{"id":1160503,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160486\/revisions\/1160503"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1160495"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1160486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1160486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1160486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}