{"id":975659,"date":"2024-12-27T06:15:10","date_gmt":"2024-12-26T22:15:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/975659.html"},"modified":"2024-12-27T06:15:12","modified_gmt":"2024-12-26T22:15:12","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5dta%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/975659.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u5165dta\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200806\/3454cfd8-98af-40d5-b95e-62b10b6f7530.webp\" alt=\"python\u5982\u4f55\u5bfc\u5165dta\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u5bfc\u5165dta\u6587\u4ef6\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528pandas\u5e93\u7684read_stata\u51fd\u6570\u3001\u4f7f\u7528Pyreadstat\u5e93\u3001\u4ee5\u53ca\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u8f6c\u6362\u4e3a\u5176\u4ed6\u683c\u5f0f\u3002<\/strong> \u5176\u4e2d\uff0c<strong>pandas\u5e93\u7684read_stata\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5<\/strong>\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\uff0c\u540c\u65f6\u652f\u6301\u591a\u79cd\u6570\u636e\u5904\u7406\u64cd\u4f5c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528pandas\u5e93\u5bfc\u5165dta\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PANDAS\u5e93\u7684READ_STATA\u51fd\u6570<\/p>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\uff0c\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u5404\u79cd\u6570\u636e\u683c\u5f0f\u3002\u5176\u4e2d\uff0cread_stata\u51fd\u6570\u4e13\u95e8\u7528\u4e8e\u8bfb\u53d6Stata\u7684dta\u6587\u4ef6\u683c\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Pandas\u5e93<\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528pandas\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528read_stata\u51fd\u6570<\/li>\n<\/ol>\n<p><p>\u5728\u5b89\u88c5\u597dpandas\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528read_stata\u51fd\u6570\u6765\u8bfb\u53d6dta\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6dta\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_stata(&#39;file.dta&#39;)<\/p>\n<h2><strong>\u663e\u793a\u6570\u636e\u7684\u524d\u51e0\u884c<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86pandas\u5e93\uff0c\u7136\u540e\u4f7f\u7528read_stata\u51fd\u6570\u8bfb\u53d6\u4e86\u4e00\u4e2a\u540d\u4e3a&#39;file.dta&#39;\u7684Stata\u6587\u4ef6\u3002\u6700\u540e\uff0c\u4f7f\u7528head\u65b9\u6cd5\u67e5\u770b\u4e86\u6570\u636e\u7684\u524d\u51e0\u884c\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u53c2\u6570\u8bbe\u7f6e<\/li>\n<\/ol>\n<p><p>read_stata\u51fd\u6570\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u53c2\u6570\u8bbe\u7f6e\uff0c\u4ee5\u4fbf\u66f4\u7075\u6d3b\u5730\u8bfb\u53d6\u6570\u636e\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>convert_categoricals<\/code>\uff1a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e3aTrue\uff0c\u5c06Stata\u7684\u7c7b\u522b\u53d8\u91cf\u8f6c\u6362\u4e3apandas\u7684Categorical\u7c7b\u578b\u3002<\/li>\n<li><code>preserve_dtypes<\/code>\uff1a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e3aTrue\uff0c\u5c3d\u53ef\u80fd\u4fdd\u7559\u539f\u59cb\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<li><code>convert_dates<\/code>\uff1a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e3aTrue\uff0c\u5c06Stata\u65e5\u671f\u8f6c\u6362\u4e3apandas\u7684datetime\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u8fd9\u4e9b\u53c2\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = pd.read_stata(&#39;file.dta&#39;, convert_categoricals=False, preserve_dtypes=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u8fd9\u4e9b\u53c2\u6570\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u5bf9\u8bfb\u53d6\u7684\u6570\u636e\u8fdb\u884c\u9002\u5f53\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001PYREADSTAT\u5e93<\/p>\n<\/p>\n<p><p>Pyreadstat\u662f\u4e00\u4e2a\u7528\u4e8e\u8bfb\u53d6\u548c\u5199\u5165Stata\u3001SPSS\u4ee5\u53caSAS\u6587\u4ef6\u7684Python\u5e93\u3002\u4e0epandas\u5e93\u76f8\u6bd4\uff0cPyreadstat\u63d0\u4f9b\u4e86\u66f4\u4e3a\u5e95\u5c42\u7684\u63a5\u53e3\uff0c\u9002\u5408\u9700\u8981\u66f4\u591a\u63a7\u5236\u548c\u81ea\u5b9a\u4e49\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Pyreadstat\u5e93<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Pyreadstat\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyreadstat<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528Pyreadstat\u8bfb\u53d6dta\u6587\u4ef6<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528Pyreadstat\u5e93\u8bfb\u53d6dta\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyreadstat<\/p>\n<h2><strong>\u8bfb\u53d6dta\u6587\u4ef6<\/strong><\/h2>\n<p>df, meta = pyreadstat.read_dta(&#39;file.dta&#39;)<\/p>\n<h2><strong>\u663e\u793a\u6570\u636e\u7684\u524d\u51e0\u884c<\/strong><\/h2>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528read_dta\u51fd\u6570\u8bfb\u53d6\u4e86dta\u6587\u4ef6\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2aDataFrame\u5bf9\u8c61\u548c\u4e00\u4e2a\u5143\u6570\u636e\u5bf9\u8c61\u3002DataFrame\u5bf9\u8c61\u5305\u542b\u4e86\u6570\u636e\u5185\u5bb9\uff0c\u800c\u5143\u6570\u636e\u5bf9\u8c61\u5219\u5305\u542b\u4e86\u6570\u636e\u7684\u63cf\u8ff0\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u5143\u6570\u636e\u7684\u4f7f\u7528<\/li>\n<\/ol>\n<p><p>Pyreadstat\u5e93\u7684\u4e00\u4e2a\u7279\u70b9\u662f\u53ef\u4ee5\u83b7\u53d6\u6570\u636e\u7684\u8be6\u7ec6\u5143\u4fe1\u606f\uff0c\u4f8b\u5982\u53d8\u91cf\u6807\u7b7e\u3001\u503c\u6807\u7b7e\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5370\u53d8\u91cf\u6807\u7b7e<\/p>\n<p>print(meta.column_labels)<\/p>\n<h2><strong>\u6253\u5370\u503c\u6807\u7b7e<\/strong><\/h2>\n<p>print(meta.value_labels)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u5728\u9700\u8981\u5206\u6790\u6570\u636e\u7ed3\u6784\u6216\u8005\u8fdb\u884c\u590d\u6742\u6570\u636e\u5904\u7406\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u8f6c\u6362\u683c\u5f0f<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u9700\u8981\u5728\u4e0d\u4f7f\u7528Python\u5e93\u7684\u60c5\u51b5\u4e0b\u8bfb\u53d6dta\u6587\u4ef6\u7684\u60c5\u51b5\uff0c\u8fd9\u65f6\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u5c06dta\u6587\u4ef6\u8f6c\u6362\u4e3a\u5176\u4ed6\u683c\u5f0f\uff08\u5982CSV\u3001Excel\u7b49\uff09\uff0c\u7136\u540e\u518d\u4f7f\u7528\u76f8\u5e94\u7684\u5e93\u8fdb\u884c\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528Stat\/Transfer\u5de5\u5177<\/li>\n<\/ol>\n<p><p>Stat\/Transfer\u662f\u4e00\u4e2a\u8457\u540d\u7684\u6570\u636e\u8f6c\u6362\u5de5\u5177\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06dta\u6587\u4ef6\u8f6c\u6362\u4e3a\u591a\u79cd\u683c\u5f0f\u3002\u5b89\u88c5\u5e76\u6253\u5f00Stat\/Transfer\u540e\uff0c\u9009\u62e9\u8f93\u5165\u6587\u4ef6\u4e3adta\u683c\u5f0f\uff0c\u8f93\u51fa\u6587\u4ef6\u4e3aCSV\u6216\u5176\u4ed6\u683c\u5f0f\uff0c\u7136\u540e\u6267\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528R\u8bed\u8a00\u8fdb\u884c\u8f6c\u6362<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u4f60\u719f\u6089R\u8bed\u8a00\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528R\u4e2d\u7684foreign\u5305\u6216haven\u5305\u6765\u8bfb\u53d6dta\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5bfc\u51fa\u4e3aCSV\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-R\"># \u4f7f\u7528haven\u5305<\/p>\n<p>library(haven)<\/p>\n<p>data &lt;- read_dta(&#39;file.dta&#39;)<\/p>\n<p>write.csv(data, &#39;file.csv&#39;)<\/p>\n<h2><strong>\u4f7f\u7528foreign\u5305<\/strong><\/h2>\n<p>library(foreign)<\/p>\n<p>data &lt;- read.dta(&#39;file.dta&#39;)<\/p>\n<p>write.csv(data, &#39;file.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b8c\u6210\u8f6c\u6362\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684pandas\u5e93\u8bfb\u53d6\u8f6c\u6362\u540e\u7684CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5bfc\u5165dta\u6587\u4ef6\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528pandas\u5e93\u7684read_stata\u51fd\u6570\u3001\u4f7f\u7528Pyreadstat\u5e93\uff0c\u4ee5\u53ca\u901a\u8fc7\u7b2c\u4e09\u65b9\u5de5\u5177\u8f6c\u6362\u683c\u5f0f\u3002<strong>\u5176\u4e2d\uff0cpandas\u5e93\u7684read_stata\u51fd\u6570\u56e0\u5176\u7b80\u5355\u6613\u7528\u548c\u529f\u80fd\u5f3a\u5927\u800c\u88ab\u5e7f\u6cdb\u4f7f\u7528<\/strong>\u3002\u5728\u9009\u62e9\u65b9\u6cd5\u65f6\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u6570\u636e\u5904\u7406\u590d\u6742\u5ea6\u8fdb\u884c\u9009\u62e9\u3002\u540c\u65f6\uff0c\u7406\u89e3\u6570\u636e\u7684\u7ed3\u6784\u548c\u683c\u5f0f\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528Python\u5de5\u5177\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff0c\u662f\u63d0\u5347\u6570\u636e\u5904\u7406\u6548\u7387\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6dta\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u8bfb\u53d6dta\u6587\u4ef6\uff0c\u901a\u5e38\u4f7f\u7528<code>pandas<\/code>\u5e93\u4e2d\u7684<code>read_stata()<\/code>\u51fd\u6570\u3002\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u8fc7\u7a0b\uff0c\u60a8\u53ea\u9700\u8981\u5b89\u88c5<code>pandas<\/code>\u5e93\u5e76\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = pd.read_stata(&#39;your_file.dta&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u5c06dta\u6587\u4ef6\u7684\u5185\u5bb9\u52a0\u8f7d\u5230\u4e00\u4e2aDataFrame\u4e2d\uff0c\u65b9\u4fbf\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5bfc\u5165dta\u6587\u4ef6\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u5bfc\u5165dta\u6587\u4ef6\u65f6\uff0c\u8bf7\u786e\u4fdd\u6587\u4ef6\u7684\u8def\u5f84\u6b63\u786e\uff0c\u5e76\u4e14\u60a8\u5df2\u7ecf\u5b89\u88c5\u4e86<code>pandas<\/code>\u5e93\u3002\u5982\u679cdta\u6587\u4ef6\u8f83\u5927\uff0c\u53ef\u80fd\u4f1a\u9700\u8981\u8f83\u957f\u7684\u52a0\u8f7d\u65f6\u95f4\u3002\u6b64\u5916\uff0c\u4e86\u89e3dta\u6587\u4ef6\u7684\u7248\u672c\u4e5f\u662f\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u4e0d\u540c\u7248\u672c\u7684dta\u6587\u4ef6\u53ef\u80fd\u4f1a\u5f71\u54cd\u6570\u636e\u7684\u8bfb\u53d6\u65b9\u5f0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5bfc\u5165\u540e\u7684dta\u6587\u4ef6\u6570\u636e\uff1f<\/strong><br \/>\u4e00\u65e6\u6210\u529f\u5bfc\u5165dta\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u63d0\u4f9b\u7684\u5404\u79cd\u529f\u80fd\u6765\u5904\u7406\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>data.head()<\/code>\u67e5\u770b\u524d\u51e0\u884c\u6570\u636e\uff0c\u6216\u8005\u4f7f\u7528<code>data.describe()<\/code>\u83b7\u53d6\u6570\u636e\u7684\u7edf\u8ba1\u4fe1\u606f\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u901a\u8fc7<code>data[&#39;column_name&#39;]<\/code>\u8bbf\u95ee\u7279\u5b9a\u5217\u7684\u6570\u636e\uff0c\u8fdb\u884c\u8fdb\u4e00\u6b65\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5bfc\u5165dta\u6587\u4ef6\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528pandas\u5e93\u7684read_stata\u51fd\u6570\u3001\u4f7f\u7528Pyreadstat [&hellip;]","protected":false},"author":3,"featured_media":975664,"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\/975659"}],"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=975659"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/975659\/revisions"}],"predecessor-version":[{"id":975665,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/975659\/revisions\/975665"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/975664"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=975659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=975659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=975659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}