{"id":1109535,"date":"2025-01-08T17:11:03","date_gmt":"2025-01-08T09:11:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1109535.html"},"modified":"2025-01-08T17:11:05","modified_gmt":"2025-01-08T09:11:05","slug":"python%e5%a6%82%e4%bd%95%e5%90%88%e5%b9%b6%e4%b8%a4%e4%b8%aacsv%e6%96%87%e4%bb%b6%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1109535.html","title":{"rendered":"python\u5982\u4f55\u5408\u5e76\u4e24\u4e2acsv\u6587\u4ef6\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072744\/536b1c4d-893d-4515-acd4-25391b8526d1.webp\" alt=\"python\u5982\u4f55\u5408\u5e76\u4e24\u4e2acsv\u6587\u4ef6\u5185\u5bb9\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5408\u5e76\u4e24\u4e2aCSV\u6587\u4ef6\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u3001csv\u6a21\u5757\u3001open\u51fd\u6570\u7b49\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c<strong>pandas\u5e93\u6700\u4e3a\u5f3a\u5927\u548c\u65b9\u4fbf<\/strong>\uff0c\u5b83\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684DataFrame\u64cd\u4f5c\uff0c\u4f7f\u5f97\u5408\u5e76CSV\u6587\u4ef6\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002\u4ee5\u4e0b\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528pandas\u5e93\u6765\u5408\u5e76\u4e24\u4e2aCSV\u6587\u4ef6\uff0c\u5e76\u5c55\u793a\u5177\u4f53\u4ee3\u7801\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u4f7f\u7528Pandas\u5e93<\/h2>\n<\/p>\n<p><h3>\u5b89\u88c5Pandas\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86pandas\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8bfb\u53d6CSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528pandas\u7684<code>read_csv<\/code>\u51fd\u6570\u8bfb\u53d6\u4e24\u4e2aCSV\u6587\u4ef6\u3002\u5047\u8bbe\u6709\u4e24\u4e2aCSV\u6587\u4ef6<code>file1.csv<\/code>\u548c<code>file2.csv<\/code>\uff0c\u5b83\u4eec\u5177\u6709\u76f8\u540c\u7684\u5217\u540d\u3002<\/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>df1 = pd.read_csv(&#39;file1.csv&#39;)<\/p>\n<p>df2 = pd.read_csv(&#39;file2.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5408\u5e76CSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>concat<\/code>\u51fd\u6570\u5c06\u4e24\u4e2aDataFrame\u8fdb\u884c\u5408\u5e76\uff0c\u9ed8\u8ba4\u662f\u6309\u884c\u5408\u5e76\uff08\u5373\u5c06\u6587\u4ef6\u5185\u5bb9\u4f9d\u6b21\u8fde\u63a5\u5728\u4e00\u8d77\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5408\u5e76DataFrame<\/p>\n<p>merged_df = pd.concat([df1, df2], ignore_index=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4fdd\u5b58\u5408\u5e76\u540e\u7684CSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>to_csv<\/code>\u51fd\u6570\u5c06\u5408\u5e76\u540e\u7684DataFrame\u4fdd\u5b58\u4e3a\u65b0\u7684CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u5408\u5e76\u540e\u7684CSV\u6587\u4ef6<\/p>\n<p>merged_df.to_csv(&#39;merged_file.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<hr>\n<p><h2>\u4e8c\u3001\u4f7f\u7528CSV\u6a21\u5757<\/h2>\n<\/p>\n<p><h3>\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u5408\u5e76<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u6253\u5f00\u4e24\u4e2aCSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;file1.csv&#39;, &#39;r&#39;) as file1, open(&#39;file2.csv&#39;, &#39;r&#39;) as file2:<\/p>\n<p>    reader1 = csv.reader(file1)<\/p>\n<p>    reader2 = csv.reader(file2)<\/p>\n<p>    # \u8bfb\u53d6\u6240\u6709\u884c<\/p>\n<p>    data1 = list(reader1)<\/p>\n<p>    data2 = list(reader2)<\/p>\n<p>    # \u5408\u5e76\u6570\u636e<\/p>\n<p>    merged_data = data1 + data2[1:]  # \u8df3\u8fc7\u7b2c\u4e8c\u4e2a\u6587\u4ef6\u7684\u8868\u5934<\/p>\n<h2><strong>\u5199\u5165\u5408\u5e76\u540e\u7684\u6570\u636e\u5230\u65b0\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;merged_file.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as merged_file:<\/p>\n<p>    writer = csv.writer(merged_file)<\/p>\n<p>    writer.writerows(merged_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<hr>\n<p><h2>\u4e09\u3001\u4f7f\u7528Open\u51fd\u6570<\/h2>\n<\/p>\n<p><h3>\u9010\u884c\u8bfb\u53d6\u5e76\u5199\u5165<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6<\/p>\n<p>file1 = open(&#39;file1.csv&#39;, &#39;r&#39;)<\/p>\n<p>file2 = open(&#39;file2.csv&#39;, &#39;r&#39;)<\/p>\n<p>merged_file = open(&#39;merged_file.csv&#39;, &#39;w&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>data1 = file1.readlines()<\/p>\n<p>data2 = file2.readlines()<\/p>\n<h2><strong>\u5199\u5165\u7b2c\u4e00\u4e2a\u6587\u4ef6\u6240\u6709\u5185\u5bb9<\/strong><\/h2>\n<p>merged_file.writelines(data1)<\/p>\n<h2><strong>\u5199\u5165\u7b2c\u4e8c\u4e2a\u6587\u4ef6\u5185\u5bb9\uff08\u8df3\u8fc7\u8868\u5934\uff09<\/strong><\/h2>\n<p>merged_file.writelines(data2[1:])<\/p>\n<h2><strong>\u5173\u95ed\u6587\u4ef6<\/strong><\/h2>\n<p>file1.close()<\/p>\n<p>file2.close()<\/p>\n<p>merged_file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<hr>\n<p><h2>\u56db\u3001\u8be6\u7ec6\u89e3\u6790Pandas\u5408\u5e76\u64cd\u4f5c<\/h2>\n<\/p>\n<p><h3>\u8bfb\u53d6CSV\u6587\u4ef6\u7684\u53c2\u6570<\/h3>\n<\/p>\n<p><p><code>read_csv<\/code>\u51fd\u6570\u6709\u8bb8\u591a\u53c2\u6570\u53ef\u4ee5\u5b9a\u5236CSV\u6587\u4ef6\u7684\u8bfb\u53d6\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>filepath_or_buffer<\/code>: CSV\u6587\u4ef6\u8def\u5f84\u6216URL\u3002<\/li>\n<li><code>sep<\/code>: \u5206\u9694\u7b26\uff0c\u9ed8\u8ba4\u4e3a\u9017\u53f7\u3002<\/li>\n<li><code>header<\/code>: \u6307\u5b9a\u884c\u53f7\u4f5c\u4e3a\u5217\u540d\uff0c\u9ed8\u8ba4\u4e3a0\u3002<\/li>\n<li><code>names<\/code>: \u5217\u540d\u5217\u8868\u3002<\/li>\n<li><code>index_col<\/code>: \u6307\u5b9a\u7d22\u5f15\u5217\u3002<\/li>\n<li><code>usecols<\/code>: \u9700\u8981\u8bfb\u53d6\u7684\u5217\u3002<\/li>\n<li><code>dtype<\/code>: \u5217\u7684\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<li><code>parse_dates<\/code>: \u89e3\u6790\u65e5\u671f\u5217\u3002<\/li>\n<\/ul>\n<p><h3>\u5408\u5e76\u64cd\u4f5c\u7684\u8be6\u7ec6\u89e3\u91ca<\/h3>\n<\/p>\n<ul>\n<li><code>pd.concat([df1, df2])<\/code>: \u9ed8\u8ba4\u6309\u884c\u5408\u5e76\uff0c\u5373\u7eb5\u5411\u5408\u5e76DataFrame\u3002<\/li>\n<li><code>ignore_index=True<\/code>: \u5ffd\u7565\u539f\u7d22\u5f15\uff0c\u91cd\u65b0\u751f\u6210\u7d22\u5f15\u3002<\/li>\n<li><code>axis=1<\/code>: \u6309\u5217\u5408\u5e76\uff0c\u5373\u6a2a\u5411\u5408\u5e76DataFrame\u3002<\/li>\n<\/ul>\n<p><h3>\u4fdd\u5b58CSV\u6587\u4ef6\u7684\u53c2\u6570<\/h3>\n<\/p>\n<p><p><code>to_csv<\/code>\u51fd\u6570\u7684\u53c2\u6570\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>path_or_buf<\/code>: \u6587\u4ef6\u8def\u5f84\u6216\u5bf9\u8c61\u3002<\/li>\n<li><code>sep<\/code>: \u5206\u9694\u7b26\u3002<\/li>\n<li><code>index<\/code>: \u662f\u5426\u4fdd\u5b58\u7d22\u5f15\uff0c\u9ed8\u8ba4\u4e3aTrue\u3002<\/li>\n<li><code>columns<\/code>: \u9700\u8981\u4fdd\u5b58\u7684\u5217\u3002<\/li>\n<li><code>header<\/code>: \u662f\u5426\u4fdd\u5b58\u5217\u540d\uff0c\u9ed8\u8ba4\u4e3aTrue\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u6309\u5217\u5408\u5e76\u4e24\u4e2aCSV\u6587\u4ef6<\/p>\n<p>merged_df = pd.concat([df1, df2], axis=1)<\/p>\n<p>merged_df.to_csv(&#39;merged_file_columns.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6570\u636e\u6e05\u6d17\u4e0e\u9884\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5408\u5e76CSV\u6587\u4ef6\u524d\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u6d17\u4e0e\u9884\u5904\u7406\uff0c\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u53bb\u9664\u7a7a\u884c\u3001\u7a7a\u5217\u3002<\/li>\n<li>\u5904\u7406\u7f3a\u5931\u503c\u3002<\/li>\n<li>\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u3002<\/li>\n<li>\u53bb\u91cd\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u6570\u636e\u6e05\u6d17\u4e0e\u9884\u5904\u7406\u793a\u4f8b<\/p>\n<p>df1.dropna(inplace=True)  # \u53bb\u9664\u7a7a\u884c<\/p>\n<p>df2.fillna(0, inplace=True)  # \u586b\u5145\u7f3a\u5931\u503c<\/p>\n<p>df1[&#39;column&#39;] = df1[&#39;column&#39;].astype(int)  # \u6570\u636e\u7c7b\u578b\u8f6c\u6362<\/p>\n<p>df1.drop_duplicates(inplace=True)  # \u53bb\u91cd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5408\u5e76\u540e\u7684\u6570\u636e\u9a8c\u8bc1<\/h3>\n<\/p>\n<p><p>\u5408\u5e76CSV\u6587\u4ef6\u540e\uff0c\u9a8c\u8bc1\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u6b63\u786e\u6027\u975e\u5e38\u91cd\u8981\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u8fdb\u884c\u9a8c\u8bc1\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u67e5\u770b\u6570\u636e\u6982\u89c8\uff1a\u4f7f\u7528<code>head()<\/code>\u3001<code>t<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l()<\/code>\u3001<code>info()<\/code>\u7b49\u51fd\u6570\u3002<\/li>\n<li>\u68c0\u67e5\u6570\u636e\u7edf\u8ba1\u4fe1\u606f\uff1a\u4f7f\u7528<code>describe()<\/code>\u51fd\u6570\u3002<\/li>\n<li>\u9a8c\u8bc1\u6570\u636e\u4e00\u81f4\u6027\uff1a\u68c0\u67e5\u884c\u6570\u3001\u5217\u6570\u3001\u6570\u636e\u7c7b\u578b\u7b49\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u6570\u636e\u9a8c\u8bc1\u793a\u4f8b<\/p>\n<p>print(merged_df.head())<\/p>\n<p>print(merged_df.info())<\/p>\n<p>print(merged_df.describe())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u5408\u5e76\u540e\u7684CSV\u6587\u4ef6\u5185\u5bb9\u5b8c\u6574\u3001\u6b63\u786e\u4e14\u4e00\u81f4\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e94\u3001\u5408\u5e76\u8fc7\u7a0b\u4e2d\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/h2>\n<\/p>\n<p><h3>\u5217\u540d\u4e0d\u4e00\u81f4<\/h3>\n<\/p>\n<p><p>\u5f53\u4e24\u4e2aCSV\u6587\u4ef6\u7684\u5217\u540d\u4e0d\u4e00\u81f4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>rename<\/code>\u51fd\u6570\u7edf\u4e00\u5217\u540d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df2.rename(columns={&#39;old_name&#39;: &#39;new_name&#39;}, inplace=True)<\/p>\n<p>merged_df = pd.concat([df1, df2], ignore_index=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6570\u636e\u7c7b\u578b\u4e0d\u4e00\u81f4<\/h3>\n<\/p>\n<p><p>\u5f53\u4e24\u4e2aCSV\u6587\u4ef6\u7684\u6570\u636e\u7c7b\u578b\u4e0d\u4e00\u81f4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>astype<\/code>\u51fd\u6570\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df1[&#39;column&#39;] = df1[&#39;column&#39;].astype(str)<\/p>\n<p>df2[&#39;column&#39;] = df2[&#39;column&#39;].astype(str)<\/p>\n<p>merged_df = pd.concat([df1, df2], ignore_index=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5904\u7406\u91cd\u590d\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5408\u5e76\u540e\u53ef\u80fd\u5b58\u5728\u91cd\u590d\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>drop_duplicates<\/code>\u51fd\u6570\u53bb\u9664\u91cd\u590d\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">merged_df.drop_duplicates(inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6309\u7279\u5b9a\u5217\u5408\u5e76<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u6309\u7279\u5b9a\u5217\u8fdb\u884c\u5408\u5e76\uff0c\u53ef\u4ee5\u4f7f\u7528<code>merge<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">merged_df = pd.merge(df1, df2, on=&#39;key_column&#39;, how=&#39;outer&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>on<\/code>: \u6307\u5b9a\u7528\u4e8e\u5408\u5e76\u7684\u5217\u3002<\/li>\n<li><code>how<\/code>: \u5408\u5e76\u65b9\u5f0f\uff0c\u53d6\u503c\u5305\u62ec<code>left<\/code>\u3001<code>right<\/code>\u3001<code>outer<\/code>\u3001<code>inner<\/code>\u3002<\/li>\n<\/ul>\n<hr>\n<p><h2>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u6848\u4f8b<\/h2>\n<\/p>\n<p><h3>\u6848\u4f8b1\uff1a\u5408\u5e76\u9500\u552e\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6709\u4e24\u4e2aCSV\u6587\u4ef6<code>sales1.csv<\/code>\u548c<code>sales2.csv<\/code>\uff0c\u5206\u522b\u8bb0\u5f55\u4e86\u4e0d\u540c\u65f6\u95f4\u6bb5\u7684\u9500\u552e\u6570\u636e\uff0c\u5305\u542b\u5217<code>Date<\/code>\u3001<code>Product<\/code>\u3001<code>Quantity<\/code>\u3001<code>Price<\/code>\u3002\u9700\u8981\u5408\u5e76\u8fd9\u4e24\u4e2a\u6587\u4ef6\u5e76\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3002<\/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>sales1 = pd.read_csv(&#39;sales1.csv&#39;)<\/p>\n<p>sales2 = pd.read_csv(&#39;sales2.csv&#39;)<\/p>\n<h2><strong>\u6570\u636e\u6e05\u6d17<\/strong><\/h2>\n<p>sales1.dropna(inplace=True)<\/p>\n<p>sales2.dropna(inplace=True)<\/p>\n<p>sales1.drop_duplicates(inplace=True)<\/p>\n<p>sales2.drop_duplicates(inplace=True)<\/p>\n<h2><strong>\u5408\u5e76CSV\u6587\u4ef6<\/strong><\/h2>\n<p>merged_sales = pd.concat([sales1, sales2], ignore_index=True)<\/p>\n<h2><strong>\u4fdd\u5b58\u5408\u5e76\u540e\u7684CSV\u6587\u4ef6<\/strong><\/h2>\n<p>merged_sales.to_csv(&#39;merged_sales.csv&#39;, index=False)<\/p>\n<h2><strong>\u6570\u636e\u9a8c\u8bc1<\/strong><\/h2>\n<p>print(merged_sales.head())<\/p>\n<p>print(merged_sales.info())<\/p>\n<p>print(merged_sales.describe())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6848\u4f8b2\uff1a\u5408\u5e76\u5b66\u751f\u6210\u7ee9\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6709\u4e24\u4e2aCSV\u6587\u4ef6<code>grades1.csv<\/code>\u548c<code>grades2.csv<\/code>\uff0c\u5206\u522b\u8bb0\u5f55\u4e86\u4e0d\u540c\u73ed\u7ea7\u7684\u5b66\u751f\u6210\u7ee9\uff0c\u5305\u542b\u5217<code>StudentID<\/code>\u3001<code>Name<\/code>\u3001<code>Subject<\/code>\u3001<code>Grade<\/code>\u3002\u9700\u8981\u6309\u5b66\u751fID\u5408\u5e76\u8fd9\u4e24\u4e2a\u6587\u4ef6\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u5b66\u751f\u7684\u5e73\u5747\u6210\u7ee9\u3002<\/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>grades1 = pd.read_csv(&#39;grades1.csv&#39;)<\/p>\n<p>grades2 = pd.read_csv(&#39;grades2.csv&#39;)<\/p>\n<h2><strong>\u5408\u5e76CSV\u6587\u4ef6<\/strong><\/h2>\n<p>merged_grades = pd.concat([grades1, grades2], ignore_index=True)<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9<\/strong><\/h2>\n<p>average_grades = merged_grades.groupby(&#39;StudentID&#39;)[&#39;Grade&#39;].mean().reset_index()<\/p>\n<h2><strong>\u4fdd\u5b58\u5e73\u5747\u6210\u7ee9\u5230CSV\u6587\u4ef6<\/strong><\/h2>\n<p>average_grades.to_csv(&#39;average_grades.csv&#39;, index=False)<\/p>\n<h2><strong>\u6570\u636e\u9a8c\u8bc1<\/strong><\/h2>\n<p>print(average_grades.head())<\/p>\n<p>print(average_grades.info())<\/p>\n<p>print(average_grades.describe())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6848\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u4f7f\u7528Python\u5408\u5e76CSV\u6587\u4ef6\u4e0d\u4ec5\u9ad8\u6548\u4fbf\u6377\uff0c\u800c\u4e14\u53ef\u4ee5\u7ed3\u5408\u6570\u636e\u6e05\u6d17\u3001\u9884\u5904\u7406\u3001\u6570\u636e\u9a8c\u8bc1\u7b49\u6b65\u9aa4\uff0c\u786e\u4fdd\u5408\u5e76\u540e\u7684\u6570\u636e\u5b8c\u6574\u3001\u6b63\u786e\u548c\u4e00\u81f4\u3002\u5e0c\u671b\u672c\u6587\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u591a\u4e2aCSV\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>pandas<\/code>\u5e93\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u591a\u4e2aCSV\u6587\u4ef6\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>pandas.read_csv()<\/code>\u51fd\u6570\u8bfb\u53d6\u6bcf\u4e2a\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>pd.concat()<\/code>\u5c06\u5b83\u4eec\u5408\u5e76\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5c06\u591a\u4e2a\u6587\u4ef6\u7684\u8def\u5f84\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u968f\u540e\u4f7f\u7528\u5faa\u73af\u8bfb\u53d6\u5e76\u5408\u5e76\u5b83\u4eec\u3002<\/p>\n<p><strong>\u5408\u5e76CSV\u6587\u4ef6\u65f6\u5982\u4f55\u5904\u7406\u91cd\u590d\u6570\u636e\uff1f<\/strong><br \/>\u5728\u5408\u5e76CSV\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u91cd\u590d\u7684\u6570\u636e\u884c\u3002\u4f7f\u7528<code>pandas<\/code>\u7684<code>drop_duplicates()<\/code>\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u60a8\u53bb\u9664\u91cd\u590d\u884c\u3002\u6b64\u5916\uff0c\u60a8\u53ef\u4ee5\u5728\u5408\u5e76\u6570\u636e\u65f6\u4f7f\u7528<code>how<\/code>\u53c2\u6570\u6307\u5b9a\u5408\u5e76\u7684\u65b9\u5f0f\uff0c\u4f8b\u5982<code>inner<\/code>\u3001<code>outer<\/code>\u7b49\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u5904\u7406\u91cd\u590d\u6570\u636e\u3002<\/p>\n<p><strong>\u5408\u5e76\u540e\u7684CSV\u6587\u4ef6\u5982\u4f55\u4fdd\u5b58\uff1f<\/strong><br \/>\u5408\u5e76\u5b8c\u6210\u540e\uff0c\u4f7f\u7528<code>pandas.to_csv()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u5408\u5e76\u540e\u7684\u6570\u636e\u4fdd\u5b58\u4e3a\u65b0\u7684CSV\u6587\u4ef6\u3002\u60a8\u53ef\u4ee5\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u5176\u4ed6\u53c2\u6570\uff0c\u4f8b\u5982<code>index=False<\/code>\u6765\u907f\u514d\u4fdd\u5b58\u884c\u7d22\u5f15\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>encoding<\/code>\u53c2\u6570\u6765\u786e\u4fdd\u6587\u4ef6\u4ee5\u6240\u9700\u7684\u7f16\u7801\u683c\u5f0f\u4fdd\u5b58\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5408\u5e76\u4e24\u4e2aCSV\u6587\u4ef6\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u3001csv\u6a21\u5757\u3001open\u51fd\u6570\u7b49\u65b9\u6cd5\u3002 \u5176\u4e2d\uff0cpan [&hellip;]","protected":false},"author":3,"featured_media":1109542,"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\/1109535"}],"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=1109535"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109535\/revisions"}],"predecessor-version":[{"id":1109544,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109535\/revisions\/1109544"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1109542"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1109535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1109535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1109535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}