import pandas as pd import re # 指定CSV文件路径 df = pd.read_csv('files/EX.csv', encoding='gbk') #print(df.columns) #df['new_column'] = df['分录摘要'].str.split('-').str[-1] #df.to_csv('files/EX_with_new_column.csv', index=False) #连续6位数字 pattern = r'\d{6,}' column='管理对象' #df['new'] = df[column].fillna('').str.replace(pattern, '') df['项目名称'] = df[df[column].str.contains('支行', na=False) == False][column] column2 ='分录摘要' df.loc[df[column2].str.contains('KPD', na=False), 'kpd'] = df[df[column2].str.contains('KPD', na=False)][ column2].str.replace("-", "") df['bianhao'] = df["kpd"].str.extract(r"KPD(\d+)") #df['项目编号'] = 'KPD' + df['bianhao'].astype(str) df['项目编号'] = df['bianhao'].apply(lambda x: f"KPD{x}" if pd.notna(x) else None) df.to_csv('files/gldx.csv', index=False)