pythonpandasbioservices

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

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