需求
这篇是上一篇的进阶版,不仅要拆分数据,还要根据aaa列进行行的筛选。这里要保留包含“Pathogenic”的行,像是“aaaPathogenic”、“Pathogenic”都要保留下来。
实现
主要代码如下:
import pandas as pd
import openpyxl
df = pd.read_excel(r"test.xls")
pattern = df['aaa'].str.contains("^Pathogenic$") ##这里
df_new = df[pattern] ##还有这里
df_new['Gene'].unique()
for p in df_new['Gene'].unique():
df_new.loc[df_new['Gene'] == p].to_excel(f'F:/流程/BRCA/2. 数据库版本整理/df_new/{p}_new.xls', index=False)