<p><em>solution from @sören:</em></p>
<p>I came up with this solution for my problem:</p>
<pre><code>from Bio.KEGG import REST as kegg

def _get_kegg(kegg_id):
    kegg_output = kegg.kegg_get(kegg_id).read()
    results = {}
    for line in kegg_output.split('\n'):
        splits = line.split()
        if not line.startswith(' '):    
            if len(splits) &gt; 0:
                key = splits[0]
                value = ' '.join(splits[1:])
                results[key] = value
        else:
            results[key] += ' '.join(splits)
    return pd.DataFrame(results, index=[kegg_id])


_get_kegg_v = np.vectorize(_get_kegg)

def get_kegg_info(kegg_ids):
    if isinstance(kegg_ids, str):
        kegg_ids = [kegg_ids]
    return pd.concat(_get_kegg_v(kegg_ids), sort=False)
</code></pre>

CC BY-SA 4.0
2021-05-06T10:25:50.830Z
2021-05-06T10:25:50.830Z
gringer
<p>Biopython provides a (bit unintuitive) API to access to the Kyoto Encyclopedia of Genes and Genomes (<a href="https://www.genome.jp/kegg/" rel="nofollow noreferrer">KEGG</a>). I am trying to make use of it, but the output is quite unhandy as a string. What is the best way to parse the data into a <code>pandas.DataFrame()</code>?</p>
<pre><code>from Bio.KEGG import REST as Kegg
import io

pd.read_table(kegg.kegg_get('K02545'))
</code></pre>

<python><kegg><pandas>
How to load output from the kegg API in biopython into a pandas dataframe?
# How to load output from the kegg API in biopython into a pandas dataframe?

(Asked by: username_0 on 2019-10-08)

Biopython provides a (bit unintuitive) API to access to the Kyoto Encyclopedia of Genes and Genomes (KEGG). I am trying to make use of it, but the output is quite unhandy as a string. What is the best way to parse the data into a pandas.DataFrame()?
from Bio.KEGG import REST as Kegg
import io

pd.read_table(kegg.kegg_get('K02545'))

**Tags:** python, kegg, pandas

---

## 1 answers

### Answer by username_1 on 2021-05-06. Score: 1

solution from @sören:
I came up with this solution for my problem:
from Bio.KEGG import REST as kegg

def _get_kegg(kegg_id):
    kegg_output = kegg.kegg_get(kegg_id).read()
    results = {}
    for line in kegg_output.split('\n'):
        splits = line.split()
        if not line.startswith(' '):    
            if len(splits) > 0:
                key = splits[0]
                value = ' '.join(splits[1:])
                results[key] = value
        else:
            results[key] += ' '.join(splits)
    return pd.DataFrame(results, index=[kegg_id])


_get_kegg_v = np.vectorize(_get_kegg)

def get_kegg_info(kegg_ids):
    if isinstance(kegg_ids, str):
        kegg_ids = [kegg_ids]
    return pd.concat(_get_kegg_v(kegg_ids), sort=False)


---

