{"id":4778,"date":"2022-07-30T20:41:15","date_gmt":"2022-07-30T12:41:15","guid":{"rendered":"https:\/\/seitlab.com\/?p=4778"},"modified":"2022-07-31T17:11:48","modified_gmt":"2022-07-31T09:11:48","slug":"2-2-python%e6%95%b0%e6%8d%ae%e5%af%bc%e5%85%a5%e4%b8%8e%e5%af%bc%e5%87%ba","status":"publish","type":"post","link":"https:\/\/www.seitlab.com\/?p=4778","title":{"rendered":"2.2 Python\u6570\u636e\u5bfc\u5165\u4e0e\u5bfc\u51fa"},"content":{"rendered":"<h3>2.2.1 \u6570\u636e\u5bfc\u5165<\/h3>\n<p>\u5728Python\u6570\u636e\u5206\u6790\u4e2d\uff0cPandas\u7a0b\u5e8f\u5305\u662f\u6700\u5e38\u7528\u7684\u6570\u636e\u5206\u6790\u5de5\u5177\u3002\u56e0\u6b64\uff0c\u8fd9\u91cc\u7684\u6570\u636e\u5bfc\u5165\u4e0e\u5bfc\u51fa\uff0c\u9ed8\u8ba4\u4ee5Pandas\u4e3a\u4f8b\u3002<\/p>\n<p><strong>\u5bfc\u5165CSV\u6570\u636e<\/strong><\/p>\n<pre><code class=\"language-python\">import pandas as pd                            # \u5bfc\u5165pandas\u7a0b\u5e8f\u5305\uff0c\u547d\u540d\u4e3a\u201cpd\u201d\n\ndf01 = pd.read_csv(r&quot;.\\data\\exp11.6.csv&quot;)      # \u4f7f\u7528pandas\u8bfb\u53d6\u5b50\u76ee\u5f55\u4e0b\u7684csv\u6587\u4ef6\uff08\u672c\u5730\u76ee\u5f55\uff09\ndf02 = pd.read_csv(r&quot;https:\/\/cdn.seitlab.com\/data\/econometrics\/exp11.6.csv&quot;)  # \u4f7f\u7528pandas\u8bfb\u53d6\u94fe\u63a5\u5730\u5740\u4e2d\u7684csv\u6587\u4ef6\uff08\u5728\u7ebf\u5730\u5740\uff09<\/code><\/pre>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li>\u529f\u80fd\uff1a\u8c03\u7528pandas\u7a0b\u5e8f\u5305\u4e2d\u7684read_csv()\u51fd\u6570\uff0c\u5c06\u6570\u636e\u5bfc\u5165\uff0c\u5e76\u547d\u540d\u4e3a\u53d8\u91cf\u201cdf01\u201d\u548c\u201cdf02\u201d<\/li>\n<li>\u53d8\u91cf\u201cdf01\u201d\u548c\u201cdf02\u201d\u4e2d\uff0c\u6570\u636e\u4ee5DataFrame\u683c\u5f0f\u5b58\u50a8\u6570\u636e\u3002<\/li>\n<li>DataFrame\u662fpandas\u652f\u6301\u7684\u8868\u683c\u578b\u6570\u636e\u683c\u5f0f\uff0c\u7c7b\u4f3cexcel\u3002<\/li>\n<li>\u5173\u4e8eDataFrame\uff0c\u8be6\u60c5\u53c2\u8003\uff1a<a href=\"https:\/\/www.runoob.com\/pandas\/pandas-dataframe.html\">Pandas \u6570\u636e\u7ed3\u6784 &#8211; DataFrame<\/a><\/li>\n<li>\u201c#\u201d\u540e\u7684\u5185\u5bb9\uff0c\u8868\u793a\u4ee3\u7801\u7684\u6ce8\u91ca\uff1b\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u6ce8\u91ca\u4e0d\u4f1a\u88ab\u6267\u884c\u3002<\/li>\n<li>\u8c03\u7528\u8bfb\u53d6csv\u6570\u636e\u7684\u51fd\u6570\uff1a<strong>pd.read_csv()<\/strong><\/li>\n<li>r\u8868\u793a\u4fdd\u6301\u53cc\u5f15\u53f7\u4e2d\u6587\u4ef6\u8def\u5f84\u7684\u201c\\\u201d\u4fdd\u6301\u539f\u6765\u7684\u542b\u4e49\u3002<sup id=\"fnref1:1\"><a href=\"#fn:1\" class=\"footnote-ref\">1<\/a><\/sup><\/li>\n<li>python\u7f16\u7a0b\u4e2d\uff0c\u201c.\/\u201d\u8868\u793a\u5f53\u524d\u76ee\u5f55\uff0c\u5373python\u7a0b\u5e8f\u5b58\u50a8\u7684\u76ee\u5f55\u3002<\/li>\n<li>\u201c.\/data\/exp11.6.csv\u201d\u8868\u793a\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u5b50\u76ee\u5f55\u201cdata\u201d\u4e2d\u540d\u79f0\u4e3a\u201cexp11.6.csv\u201d\u6587\u4ef6<\/li>\n<\/ul>\n<p><strong>\u5bfc\u5165Excel\u6570\u636e<\/strong><\/p>\n<pre><code class=\"language-python\">import pandas as pd                            # \u5bfc\u5165pandas\u7a0b\u5e8f\u5305\uff0c\u547d\u540d\u4e3a\u201cpd\u201d\n\ndf01 = pd.read_excel(r&quot;.\\data\\exp11.6.xlsx&quot;,sheet_name=&quot;sheet1&quot;,nrows=12,usecols=[1,2,3,4,5,6])    # \u4f7f\u7528pandas\u8bfb\u53d6\u5b50\u76ee\u5f55\u4e0b\u7684excel\u6587\u4ef6\uff08\u672c\u5730\u76ee\u5f55\uff09\ndf02 = pd.read_excel(r&quot;https:\/\/cdn.seitlab.com\/data\/econometrics\/exp11.6.xlsx&quot;)  # \u4f7f\u7528pandas\u8bfb\u53d6\u94fe\u63a5\u5730\u5740\u4e2d\u7684excel\u6587\u4ef6\uff08\u5728\u7ebf\u5730\u5740\uff09<\/code><\/pre>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li>\u529f\u80fd\uff1a\u8c03\u7528pandas\u7a0b\u5e8f\u5305\u4e2d\u7684read_excel()\u51fd\u6570\uff0c\u5c06\u6570\u636e\u5bfc\u5165\uff0c\u5e76\u547d\u540d\u4e3a\u53d8\u91cf\u201cdf01\u201d\u548c\u201cdf02\u201d<\/li>\n<li>\u53d8\u91cf\u201cdf01\u201d\u548c\u201cdf02\u201d\u4e2d\uff0c\u6570\u636e\u4ee5DataFrame\u683c\u5f0f\u5b58\u50a8\u6570\u636e\u3002<\/li>\n<li>DataFrame\u662fpandas\u652f\u6301\u7684\u8868\u683c\u578b\u6570\u636e\u683c\u5f0f\uff0c\u7c7b\u4f3cexcel\u3002<\/li>\n<li>\u5173\u4e8eDataFrame\uff0c\u8be6\u60c5\u53c2\u8003\uff1a<a href=\"https:\/\/www.runoob.com\/pandas\/pandas-dataframe.html\">Pandas \u6570\u636e\u7ed3\u6784 &#8211; DataFrame<\/a><\/li>\n<li>\u8c03\u7528\u8bfb\u53d6excel\u6570\u636e\u7684\u51fd\u6570\uff1a<strong>pd.read_excel()<\/strong><\/li>\n<li>\u53c2\u6570\u8bf4\u660e\uff1a\u201cnrows=12\u201d\u8868\u793a\u4f7f\u7528\u524d12\u884c\uff0c\u201cusecols=[1,2,3,4,5,6]\u201d\u8868\u793a\u4f7f\u7528\u7b2c2-7\u5217<\/li>\n<li>\u201c.\/data\/exp11.6.xlsx\u201d\u8868\u793a\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u5b50\u76ee\u5f55\u201cdata\u201d\u4e2d\u540d\u79f0\u4e3a\u201cexp11.6.xlsx\u201d\u6587\u4ef6<\/li>\n<\/ul>\n<hr \/>\n<h3>2.2.2 \u6570\u636e\u5bfc\u51fa<\/h3>\n<p><strong>\u5bfc\u51fa\u4e3aCSV\u6570\u636e<\/strong><\/p>\n<pre><code class=\"language-python\">import pandas as pd                                                         # \u5bfc\u5165pandas\u7a0b\u5e8f\u5305\uff0c\u547d\u540d\u4e3a\u201cpd\u201d\n\ndf.to_csv(r&#039;D:\/python\/data\/exp11.6.csv&#039;,sheet_name=&quot;Sheet1&quot;,index=False)    # \u5bfc\u51fadf01\u6570\u636e\u5230excel\u6587\u4ef6<\/code><\/pre>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li>DataFrame\u6570\u636e\u5bfc\u51fa\u51fd\u6570\uff1a<strong>df.to_csv()<\/strong><\/li>\n<li>\u53c2\u6570\u8bf4\u660e\uff1a\u201csheet_name=&quot;Sheet1&quot; \u201d\u8868\u793a\u5bfc\u51fa\u7684excel\u9875\u9762\uff0c\u201cindex=False\u201d\u8868\u793a\u4e0d\u5bfc\u51fa\u884c\u7d22\u5f15<\/li>\n<\/ul>\n<p><strong>\u5bfc\u51fa\u4e3aExcel\u6570\u636e<\/strong><\/p>\n<p>DataFrame\u6570\u636e\u5bfc\u51fa\u5230excel\u6587\u4ef6\uff0c\u9700\u8981\u5b89\u88c5\uff1aopenpyxl\u7a0b\u5e8f\u5305\u3002<\/p>\n<pre><code class=\"language-python\">import pandas as pd                                                           # \u5bfc\u5165pandas\u7a0b\u5e8f\u5305\uff0c\u547d\u540d\u4e3a\u201cpd\u201d\n\ndf.to_excel(r&#039;D:\/python\/data\/exp11.6.xlsx&#039;,sheet_name=&quot;Sheet1&quot;,index=True)    # \u5bfc\u51fadf01\u6570\u636e\u5230excel\u6587\u4ef6<\/code><\/pre>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li>DataFrame\u6570\u636e\u5bfc\u51fa\u51fd\u6570\uff1a<strong>df.to_excel()<\/strong><\/li>\n<li>\u53c2\u6570\u8bf4\u660e\uff1a\u201csheet_name=&quot;Sheet1&quot; \u201d\u8868\u793a\u5bfc\u51fa\u7684excel\u9875\u9762\uff0c\u201cindex=True\u201d\u8868\u793a\u540c\u65f6\u5bfc\u51fa\u884c\u7d22\u5f15<\/li>\n<\/ul>\n<hr \/>\n<h3>\u53c2\u8003<\/h3>\n<ul>\n<li>\n<p><a href=\"https:\/\/www.runoob.com\/pandas\/pandas-tutorial.html\">\u83dc\u9e1f\u6559\u7a0b\uff1aPandas \u6559\u7a0b<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/www.dounaite.com\/article\/62832e4eac359fc91338d804.html\">.NET\u8c46\u5976\u7279\uff1a\u65e5\u5e38\u7b14\u8bb0&#8211;1.18(pandas\u4e4bexcel\u6570\u636e\u6587\u4ef6\u5bfc\u5165\u548c\u5bfc\u51fa\u5e38\u89c1\u95ee\u9898)<\/a><\/p>\n<\/li>\n<\/ul>\n<div class=\"footnotes\">\n<hr \/>\n<ol>\n<li id=\"fn:1\">\n<p>\u201c\\\u201d\u8868\u793a\u8f6c\u4e49\u7b26\uff0c\u5373\u5728\u67d0\u4e9b\u7b26\u53f7\u524d\u9762\u52a0\u5165\u201c\\\u201d\u8868\u793a\u7279\u6b8a\u542b\u4e49\uff08\u4f8b\u5982\uff1a\u5728Latex\u516c\u5f0f\u8f93\u5165\u4e2d\uff0c\u201c\\beta\u201d\u8868\u793a\u4e3a $\\beta$\uff09\u3002\u901a\u5e38\uff0c\u5728windows\u4e2d\uff0c\u8def\u5f84\u8868\u793a\u4e3a\u201cD:\\python\\data\u201d\uff0c\u5728linux\u4e2d\uff0c\u8def\u5f84\u8868\u793a\u4e3a\u201c\/python\/data\u201d\u3002\u5728python\u7f16\u7a0b\u4e2d\uff0c\u4e3a\u4e86\u907f\u514dwindow\u7cfb\u7edf\u4e0b\u6587\u4ef6\u8def\u5f84\u4e2d\u7684\u201c\\\u201d\u88ab\u9519\u8bef\u8bc6\u522b\u4e3a\u8f6c\u4e49\u7b26\uff0c\u9700\u8981\u5728\u6587\u4ef6\u8def\u5f84\u7684\u53cc\u5f15\u53f7\u524d\u52a0\u5165\u7edf\u4e00&quot;r&quot;\uff0c\u907f\u514d\u8bc6\u522b\u9519\u8bef\u3002&#160;<a href=\"#fnref1:1\" rev=\"footnote\" class=\"footnote-backref\">&#8617;<\/a><\/p>\n<\/li>\n<\/ol>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>2.2.1 \u6570\u636e\u5bfc\u5165 \u5728Python\u6570\u636e\u5206\u6790\u4e2d\uff0cPandas\u7a0b\u5e8f\u5305\u662f\u6700\u5e38\u7528\u7684\u6570\u636e\u5206\u6790\u5de5\u5177\u3002\u56e0\u6b64\uff0c\u8fd9\u91cc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[29,30],"tags":[],"class_list":["post-4778","post","type-post","status-publish","format-standard","hentry","category-29","category-30"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/posts\/4778","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4778"}],"version-history":[{"count":0,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/posts\/4778\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.seitlab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}