读取CSV文件时出现SyntaxError语法错误,请求Python代码调试帮助
调试处理CSV文件的Python代码错误
问题背景
我需要编写Python代码处理两个CSV文件,目标是找出源文件中满足**阈值(≥'T'列均值的50%)**且在目标文件中缺失的行,方便手动复制到目标文件。运行代码时触发语法错误,相关代码与报错信息如下:
原代码
import pandas as pd # Define file paths (replace with your actual paths) source_file = "C:/Users/sharsa07/Desktop/pipeline/gso_orb_ticket_list.csv" dest_file = "C:/Users/sharsa07/Desktop/pipeline/gso_pipeline.csv" # Read excel files into DataFrames df_source = pd.read_csv(C:/Users/sharsa07/Desktop/pipeline/gso_orb_ticket_list.csv) df_dest = pd.read_csv(C:/Users/sharsa07/Desktop/pipeline/gso_pipeline.csv) # Find the common column name (assuming the same column name in both files, case-insensitive) common_col = "a" # Assuming column names are the same (case-insensitive) # Merge DataFrames based on the common column (outer join to keep unmatched rows) merged_df = df_source.merge(df_dest[[common_col]], how="outer", on=common_col.lower()) # Calculate threshold value based on 'T' column mean in the source DataFrame threshold_value = df_source['T'].mean() * 0.5 # Filter merged DataFrame to rows where 'T' is greater than or equal to the threshold and source column is missing in destination filtered_df = merged_df[(merged_df['T'] >= threshold_value) & (merged_df[common_col.lower()].isna())] # Get source column names from the filtered DataFrame (excluding the common column) source_cols = set(filtered_df.columns) - {common_col.lower()} # Print the column names that meet the criteria print("Columns to be checked:", source_cols)
报错信息
Cell In[2], line 8 df_source = pd.read_csv(C:/Users/sharsa07/Desktop/pipeline/gso_orb_ticket_list.csv) ^ SyntaxError: invalid syntax
错误原因与修复方案
1. 语法错误修复
报错行的核心问题是文件路径没有用引号包裹,Python无法识别未加引号的路径字符串。另外你已经定义了source_file和dest_file变量,直接复用变量更简洁,无需重复写路径:
将原代码中读取文件的两行替换为:
df_source = pd.read_csv(source_file) df_dest = pd.read_csv(dest_file)
2. 逻辑错误修正
原代码的合并与过滤逻辑存在问题:
- 用
outer join会保留两边所有行,而我们只需要源文件中存在、目标文件中缺失的行,用left join更合适 - 判断缺失的条件应该是目标文件的对应列为空,而非公共列本身为空
同时,公共列的大小写处理需要统一,避免因列名大小写不一致导致匹配失败。
修正后的完整代码
import pandas as pd # 定义文件路径 source_file = "C:/Users/sharsa07/Desktop/pipeline/gso_orb_ticket_list.csv" dest_file = "C:/Users/sharsa07/Desktop/pipeline/gso_pipeline.csv" # 读取CSV文件为DataFrame df_source = pd.read_csv(source_file) df_dest = pd.read_csv(dest_file) # 统一列名为小写(解决大小写不匹配问题) df_source.columns = df_source.columns.str.lower() df_dest.columns = df_dest.columns.str.lower() # 指定公共匹配列(请替换为实际的唯一标识列,比如工单ID) common_col = "a" # 示例列名,需根据实际数据修改 # 左连接:保留源文件所有行,匹配目标文件的公共列 merged_df = df_source.merge(df_dest[[common_col]], how="left", on=common_col, indicator=True) # 计算阈值:T列均值的50% threshold_value = df_source['t'].mean() * 0.5 # 过滤条件:T列值≥阈值,且目标文件中无匹配行 filtered_df = merged_df[(merged_df['t'] >= threshold_value) & (merged_df['_merge'] == 'left_only')] # 输出符合条件的行(包含源文件所有列) print("需要复制到目标文件的行:") print(filtered_df.drop(columns=['_merge'])) # 如果需要保存结果到CSV # filtered_df.drop(columns=['_merge']).to_csv("missing_rows.csv", index=False)
内容的提问来源于stack exchange,提问作者Satyajit Sharma
相关产品推荐
相关产品推荐

