{"id":1091629,"date":"2025-01-08T14:13:30","date_gmt":"2025-01-08T06:13:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1091629.html"},"modified":"2025-01-08T14:13:32","modified_gmt":"2025-01-08T06:13:32","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80sas7bdat%e6%96%87%e4%bb%b6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1091629.html","title":{"rendered":"python\u5982\u4f55\u6253\u5f00sas7bdat\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205040\/e8b0900c-cd1c-4dd5-b0c2-288172ea84e5.webp\" alt=\"python\u5982\u4f55\u6253\u5f00sas7bdat\u6587\u4ef6\" \/><\/p>\n<p><p> Python\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u6765\u6253\u5f00SAS7BDAT\u6587\u4ef6\uff1a<strong>\u4f7f\u7528pandas\u5e93\u3001\u4f7f\u7528sas7bdat\u5e93\u3001\u4f7f\u7528pyreadstat\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528pandas\u5e93<\/strong> \u662f\u8f83\u4e3a\u5e38\u7528\u548c\u7b80\u4fbf\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528pandas\u5e93\u6765\u6253\u5f00SAS7BDAT\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\uff0c\u5b83\u80fd\u591f\u8f7b\u677e\u5904\u7406\u5404\u79cd\u6570\u636e\u683c\u5f0f\uff0c\u5305\u62ecSAS7BDAT\u6587\u4ef6\u3002\u8981\u4f7f\u7528Pandas\u5e93\u6765\u6253\u5f00SAS7BDAT\u6587\u4ef6\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5pandas\u548csas7bdat\u5e93\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5\u4f9d\u8d56\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p>pip install sas7bdat<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u4ee3\u7801\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>from sas7bdat import SAS7BDAT<\/p>\n<p>with SAS7BDAT(&#39;your_file.sas7bdat&#39;) as reader:<\/p>\n<p>    df = reader.to_data_frame()<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86pandas\u548cSAS7BDAT\u5e93\uff0c\u7136\u540e\u4f7f\u7528SAS7BDAT\u7c7b\u6253\u5f00SAS7BDAT\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3apandas DataFrame\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>print(df.head())<\/code>\u6253\u5370\u6570\u636e\u7684\u524d\u4e94\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528sas7bdat\u5e93<\/h3>\n<\/p>\n<p><p>SAS7BDAT\u5e93\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u8bfb\u53d6SAS7BDAT\u6587\u4ef6\u7684Python\u5e93\u3002\u867d\u7136Pandas\u5e93\u4e5f\u4f9d\u8d56\u4e8e\u5b83\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8be5\u5e93\u6765\u8bfb\u53d6SAS7BDAT\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5sas7bdat\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install sas7bdat<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u4ee3\u7801\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from sas7bdat import SAS7BDAT<\/p>\n<p>with SAS7BDAT(&#39;your_file.sas7bdat&#39;) as reader:<\/p>\n<p>    for row in reader:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u4f7f\u7528SAS7BDAT\u5e93\u6253\u5f00SAS7BDAT\u6587\u4ef6\uff0c\u5e76\u9010\u884c\u6253\u5370\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u4e8e\u6570\u636e\u91cf\u8f83\u5c0f\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pyreadstat\u5e93<\/h3>\n<\/p>\n<p><p>Pyreadstat\u5e93\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u8bfb\u53d6SAS7BDAT\u6587\u4ef6\u7684Python\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u548c\u66f4\u597d\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5pyreadstat\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyreadstat<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u4ee3\u7801\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pyreadstat<\/p>\n<p>df, meta = pyreadstat.read_sas7bdat(&#39;your_file.sas7bdat&#39;)<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528pyreadstat\u5e93\u8bfb\u53d6SAS7BDAT\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3apandas DataFrame\u3002<code>meta<\/code>\u53d8\u91cf\u5305\u542b\u4e86\u6587\u4ef6\u7684\u5143\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<p><h4>1\u3001Pandas\u5e93<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u529f\u80fd\u5f3a\u5927\u3001\u6613\u4e8e\u4f7f\u7528\u3001\u53ef\u4ee5\u5904\u7406\u5404\u79cd\u6570\u636e\u683c\u5f0f\u3002<\/p>\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a\u9700\u8981\u5b89\u88c5\u989d\u5916\u7684sas7bdat\u5e93\uff0c\u6027\u80fd\u53ef\u80fd\u4e0d\u5982pyreadstat\u5e93\u3002<\/p>\n<\/p>\n<p><h4>2\u3001SAS7BDAT\u5e93<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u4e13\u95e8\u7528\u4e8e\u8bfb\u53d6SAS7BDAT\u6587\u4ef6\uff0c\u7b80\u5355\u6613\u7528\u3002<\/p>\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a\u529f\u80fd\u8f83\u4e3a\u5355\u4e00\uff0c\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u6027\u80fd\u8f83\u5dee\u3002<\/p>\n<\/p>\n<p><h4>3\u3001Pyreadstat\u5e93<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u6027\u80fd\u4f18\u8d8a\u3001\u529f\u80fd\u4e30\u5bcc\u3001\u652f\u6301\u8bfb\u53d6\u6587\u4ef6\u7684\u5143\u6570\u636e\u3002<\/p>\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a\u4f7f\u7528\u76f8\u5bf9\u8f83\u4e3a\u590d\u6742\uff0c\u9700\u8981\u5b89\u88c5\u989d\u5916\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c<strong>\u4f7f\u7528pandas\u5e93<\/strong>\u662f\u6253\u5f00SAS7BDAT\u6587\u4ef6\u7684\u8f83\u4e3a\u63a8\u8350\u7684\u65b9\u6cd5\u3002\u5b83\u4e0d\u4ec5\u529f\u80fd\u5f3a\u5927\uff0c\u800c\u4e14\u6613\u4e8e\u4f7f\u7528\uff0c\u53ef\u4ee5\u5904\u7406\u5404\u79cd\u6570\u636e\u683c\u5f0f\u3002\u5bf9\u4e8e\u6709\u66f4\u9ad8\u6027\u80fd\u9700\u6c42\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<strong>pyreadstat\u5e93<\/strong>\u3002\u603b\u4e4b\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u5904\u7406SAS7BDAT\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6sas7bdat\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8bfb\u53d6sas7bdat\u6587\u4ef6\u901a\u5e38\u4f7f\u7528<code>pandas<\/code>\u5e93\u7ed3\u5408<code>sas7bdat<\/code>\u6216<code>pyreadstat<\/code>\u5e93\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u52a0\u8f7dSAS\u6570\u636e\u96c6\u3002\u4f7f\u7528<code>pandas<\/code>\u7684<code>read_sas<\/code>\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u8bfb\u53d6sas7bdat\u6587\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = pd.read_sas(&#39;your_file.sas7bdat&#39;)\nprint(data.head())\n<\/code><\/pre>\n<p>\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86\u76f8\u5e94\u7684\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install pandas<\/code>\u548c<code>pip install pyreadstat<\/code>\u6765\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<p><strong>\u6253\u5f00sas7bdat\u6587\u4ef6\u9700\u8981\u5b89\u88c5\u54ea\u4e9bPython\u5e93\uff1f<\/strong><br \/>\u5728\u5904\u7406sas7bdat\u6587\u4ef6\u65f6\uff0c\u6700\u5e38\u7528\u7684Python\u5e93\u5305\u62ec<code>pandas<\/code>\u548c<code>pyreadstat<\/code>\u3002<code>pandas<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u80fd\u591f\u5904\u7406\u591a\u79cd\u683c\u5f0f\u7684\u6570\u636e\u6587\u4ef6\u3002<code>pyreadstat<\/code>\u5219\u4e13\u95e8\u7528\u4e8e\u8bfb\u53d6SAS\u3001SPSS\u548cStata\u6587\u4ef6\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6587\u4ef6\u8bfb\u53d6\u80fd\u529b\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff1a  <\/p>\n<pre><code class=\"language-bash\">pip install pandas pyreadstat\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406sas7bdat\u6587\u4ef6\u4e2d\u7684\u7f3a\u5931\u503c\uff1f<\/strong><br \/>\u5728\u8bfb\u53d6sas7bdat\u6587\u4ef6\u540e\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f3a\u5931\u503c\u3002<code>pandas<\/code>\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u7f3a\u5931\u503c\uff0c\u4f8b\u5982\u4f7f\u7528<code>dropna()<\/code>\u5220\u9664\u7f3a\u5931\u6570\u636e\uff0c\u6216\u4f7f\u7528<code>fillna()<\/code>\u586b\u8865\u7f3a\u5931\u503c\u3002\u5177\u4f53\u53d6\u51b3\u4e8e\u4f60\u7684\u6570\u636e\u5206\u6790\u9700\u6c42\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\"># \u5220\u9664\u7f3a\u5931\u503c\ncleaned_data = data.dropna()\n\n# \u7528\u7279\u5b9a\u503c\u586b\u8865\u7f3a\u5931\u503c\nfilled_data = data.fillna(0)\n<\/code><\/pre>\n<p>\u6839\u636e\u5206\u6790\u76ee\u6807\uff0c\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6570\u636e\u4e2d\u7684\u7f3a\u5931\u90e8\u5206\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u6765\u6253\u5f00SAS7BDAT\u6587\u4ef6\uff1a\u4f7f\u7528pandas\u5e93\u3001\u4f7f\u7528sas7bdat\u5e93\u3001\u4f7f\u7528p [&hellip;]","protected":false},"author":3,"featured_media":1091636,"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\/1091629"}],"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=1091629"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091629\/revisions"}],"predecessor-version":[{"id":1091637,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091629\/revisions\/1091637"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1091636"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1091629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1091629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1091629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}