Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to put the output of this print(d) function into a pandas dataframe

I am working with the bioservices package in python and I want to take the output of this function and put it into a dataframe using pandas

from bioservices import UniProt
u = UniProt(verbose=False)
d = u.search("yourlist:M20211203F248CABF64506F29A91F8037F07B67D133A278O", frmt="tab", limit=5,
             columns="id, entry name")
print(d)

this is the result I am getting, almost like a neat little table

The problem however is I cannot work with the data in this form and I want to put it into a dataframe using pandas

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

trying this code below does not work and it only returns the error "ValueError: DataFrame constructor not properly called"

import pandas as pd
df = pd.DataFrame(columns= ['Entry','Entry name'],
              data=d)
print(df)

>Solution :

Use pd.read_csv, after encapsulating your output in a StringIO (to present a file-like interface):

import io
import pandas as pd

data = 'Entry\tEntry name\na\t1\nb\t2'
io_data = io.StringIO(data)

df = pd.read_csv(io_data, sep='\t')
print(df)

The output is a dataframe:

  Entry  Entry name
0     a           1
1     b           2

Sample data:

from bioservices import UniProt
import io

u = UniProt(verbose=False)
d = u.search("yourlist:M20211203F248CABF64506F29A91F8037F07B67D133A278O", frmt="tab", limit=5,
             columns="id, entry name")
#print(d)

df = pd.read_csv(io.StringIO(d), sep='\t')
print(df)
    Entry   Entry name
0  Q8TAS1  UHMK1_HUMAN
1  P35916  VGFR3_HUMAN
2  Q96SB4  SRPK1_HUMAN
3  Q6P3W7  SCYL2_HUMAN
4  Q9UKI8   TLK1_HUMAN
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading