'save as dataframe python

I am really new in python, so I am doing a consultd and I want that the results be save like dataframe instead of be just print in the terminal. Here is my code:

service = Service("https://www.mousemine.org/mousemine/service")
query = service.new_query("Gene")
query.add_view(
    "primaryIdentifier", "symbol", "organism.name",
    "homologues.homologue.primaryIdentifier", "homologues.homologue.symbol",
    "homologues.homologue.organism.name", "homologues.type",
    "homologues.dataSets.name"
)
query.add_constraint("homologues.type", "NONE OF", ["horizontal gene transfer", "least diverged horizontal gene transfer"], code = "B")
query.add_constraint("Gene", "LOOKUP", "ENSMUSG00000026981,ENSMUSG00000068039,ENSMUSG00000035007,ENSMUSG00000022972,", "M. musculus", code = "A")
query.add_constraint("homologues.homologue.organism.name", "=", "Homo sapiens", code = "C")
query.add_constraint("homologues.dataSets.name", "=", "Mouse/Human Orthologies from MGI", code = "D")

   for row in query.rows():
    print(row["primaryIdentifier"], row["symbol"], row["organism.name"], \
        row["homologues.homologue.primaryIdentifier"], 
        row["homologues.homologue.symbol"], \
        row["homologues.homologue.organism.name"], row["homologues.type"], \
        row["homologues.dataSets.name"])

And this is the result that I get it

MGI:1915251 Cfap298 Mus musculus 56683 CFAP298 Homo sapiens orthologue Mouse/Human Orthologies from MGI MGI:2144506 Rundc1 Mus musculus 146923 RUNDC1 Homo sapiens orthologue Mouse/Human Orthologies from MGI MGI:96547 Il1rn Mus musculus 3557 IL1RN Homo sapiens orthologue Mouse/Human Orthologies from MGI MGI:98535 Tcp1 Mus musculus 6950 TCP1 Homo sapiens orthologue Mouse/Human Orthologies from MGI

And it is perfectly ok, but I need it in a dataframe. And if I can the consult using a table with all the ID and not have to writing one by one (because there are 14000) that will be amazing.



Solution 1:[1]

Using a loop:

   df = pd.DataFrame({})

   for row in query.rows():
    data = {'primaryIdentifier': row["primaryIdentifier"],
            'symbol': row["symbol"],
            'organism.name': row["organism.name"],
            'homologues.homologue.primaryIdentifier': row["homologues.homologue.primaryIdentifier"],
            'homologues.homologue.symbol': row["homologues.homologue.symbol"],
            'homologues.homologue.organism.name': row["homologues.homologue.organism.name"],
            'homologues.type': row["homologues.type"],
            'homologues.dataSets.name': row["homologues.dataSets.name"]}
    df = df.append(pd.DataFrame(data), ignore_index = True)

Note that you must do an import pandas as pd and make sure that the data are str.

Solution 2:[2]

Pandas would help you.https://pandas.pydata.org. Feel free to ask questions if you stuck with something.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mario
Solution 2 Krzysztof Szewczyk