df =pd.read_excel("D:\HBVdata\HBV_P.xlsx") smile_list =df['Smiles'].tolist() mols =[Chem.MolFromSmiles(smile) for smile in smile_list] dg =pd.read_csv("D:\HBVdata\hbvfrag_recap_delete_duplicate.csv") smi_list =dg['mol'].tolist() submols =[Chem.MolFromSmarts(smi) for smi in smi_list] counts =[] for submol in submols: matches = [mol.GetSubstructMatches(submol) for mol in mols] counts.append(len(matches)) total_count = sum(counts) frequencies = [count / total_count for count in counts] for fragment, frequency in zip(smi_list, frequencies): print(f"{fragment}: {frequency}")这段代码如何将输出结果保存到excel
时间: 2024-02-27 08:58:18 浏览: 127
Read data from the Excel spreadsheet.zip_Spreadsheet::Read_excel
要将输出结果保存到Excel文件中,可以使用pandas库中的DataFrame和ExcelWriter方法。可以按照以下步骤进行修改:
1. 将print语句的输出结果存储在一个列表中,例如:
```
results = []
for fragment, frequency in zip(smi_list, frequencies):
results.append([fragment, frequency])
```
2. 将列表转换为DataFrame格式,并命名列名:
```
df_results = pd.DataFrame(results, columns=['Fragment', 'Frequency'])
```
3. 使用ExcelWriter方法将DataFrame写入Excel文件:
```
with pd.ExcelWriter('output.xlsx') as writer:
df_results.to_excel(writer, index=False)
```
其中,'output.xlsx'是你想要输出的Excel文件名,index=False表示不保存行索引。
完整代码如下:
```
import pandas as pd
from rdkit import Chem
df = pd.read_excel("D:\HBVdata\HBV_P.xlsx")
smile_list = df['Smiles'].tolist()
mols = [Chem.MolFromSmiles(smile) for smile in smile_list]
dg = pd.read_csv("D:\HBVdata\hbvfrag_recap_delete_duplicate.csv")
smi_list = dg['mol'].tolist()
submols = [Chem.MolFromSmarts(smi) for smi in smi_list]
counts = []
for submol in submols:
matches = [mol.GetSubstructMatches(submol) for mol in mols]
counts.append(len(matches))
total_count = sum(counts)
frequencies = [count / total_count for count in counts]
results = []
for fragment, frequency in zip(smi_list, frequencies):
results.append([fragment, frequency])
df_results = pd.DataFrame(results, columns=['Fragment', 'Frequency'])
with pd.ExcelWriter('output.xlsx') as writer:
df_results.to_excel(writer, index=False)
```
阅读全文