0

I am struggling to understand why this returns a list containing a DataFrame rather than only the DataFrame. Is there something wrong with the code or a way to return only the DataFrame? It works as expected if not placed within a function.

import sqlite3
import pandas as pd

def get_tbl_info(db ='MyDatabase', table ='Measurements'):

        database = "/Users/Mary/Documents/Database/{DB}.db"..format(DB=db)

        conn = sqlite3.connect(database)

        tbl_info_command = "PRAGMA TABLE_INFO({table});".format(table=table)
        result_all = pd.read_sql_query(tbl_info_command,conn)
        print(type(result_all))
        return [result_all]



out = get_tbl_info()
print(type(out))

gives:

<class 'pandas.core.frame.DataFrame'>
<class 'list'>
1
  • 3
    Because you're returning it as a list via the square brackets: return [result_all]. Try just return result_all. Commented Jan 14, 2020 at 18:09

1 Answer 1

2

Because when you enclose a variable with brackets [ ], Python understands it as "ok put this variable inside a list". Just replace the return of your function with:

return result_all

This should work properly, or actually your function could just return your dataframe directly

return pd.read_sql_query(tbl_info_command,conn)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.