0

我是编程新手。我正在尝试使用 STITCH API 查找化学品的目标。当我运行代码时,对于列表中的某些输入,我得到了输出。但最后,几行显示上面引用的索引错误(例如,如果我有 10 个输入 ID,我得到其中 7 个的输出,其他三个不会运行,因为我得到索引错误)。请在下面查看我的代码并帮助解决问题。我的输入是 PubChem CID 列表,仅此而已。

import requests
import pandas as pd
from io import StringIO

out_df = pd.DataFrame(columns=['Chemical_ids', 'Target EnsemblID'])
Path = 'boswellia_stitch_input.txt'
df = pd.read_csv(Path, sep='\t')

for row, line in enumerate(df['Pubchem_CID']):
    base_url = "http://stitch.embl.de/api/tsv/interactorsList"
    Chemical_ids = (f'CID{line}')

    params = {"identifiers": Chemical_ids, "species": "9606", "limit": "400"}

    res = requests.get(base_url, params=params)

    if res.status_code == 200:
        data = [row.split('.')[1] if '.' in row else row for row in res.text.split('\n')[1:-1]]
        result = pd.DataFrame(data[1:], columns=[data[0]])
        
        temp = pd.DataFrame()
        
        temp['Target EnsemblID'] = result.values.flatten()
        temp.reset_index(drop=True, inplace=True)
        temp['Chemical_ids'] = [line]*len(result.index)
        #temp['Chemical_name'] = [df.at[row, 'molecule_name']]*len(result.index) 
        display(temp)
        out_df = out_df.append(temp, ignore_index=True)
#display(out_df)

My error:

IndexError                                Traceback (most recent call last)
<ipython-input-14-1bb58828596d> in <module>
      9     if res.status_code == 200:
     10         data = [row.split('.')[1] if '.' in row else row for row in res.text.split('\n')[1:-1]]
---> 11         result = pd.DataFrame(data[1:], columns=[data[0]])
     12 
     13         temp = pd.DataFrame()

IndexError: list index out of range
4

0 回答 0