Reputation: 788
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'>
Upvotes: 0
Views: 245
Reputation: 115
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)
Upvotes: 2