How to load output from the kegg API in biopython into a pandas dataframe?

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


• It might be helpful if you provide an example of the output so it is easier understand why this string is not useful, and how it relates to your coded solution.
– user921
Apr 20, 2020 at 16:55

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):
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)