首页 > 解决方案 > Python global variables for DataFrames

问题描述

This is probably a beginner question since im still learning python but i can't yet figure out the right way to do this is. I have a sqlite database, that contains some tables. I want to convert the tables into pandas dataframes. Usually i do this: df = pd.read_sql_query("SELECT * FROM table")

In this example i want to create multiple dataframes dynamically from the master table:

conn = sqlite3.connect("database.sqlite")
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")

def solution_1():
    for row in cursor:
       globals()[row[0]] = pd.read_sql_query("SELECT * FROM " + row[0], conn)

def solution_2():
    for row in cursor:
        df[row[0]] = pd.read_sql_query("SELECT * FROM " + row[0], conn)

Solution 1 was the first i came up with. But everywhere i read about that, people say its a bad habit/idea to create global variables and better store them in a dictionary, so i came up with solution 2. I see the general issues of creating global variables but in my case with dataframes, the dictionary solution is kinda clunky because i would have to call the dataframe like this:

Solution 1: Table["column"].describe()
Solution 2: df["table1"]["column"] .describe()

Is it just a fact i have to accept in this situation or is there a way to deal with this better? And another question is: In this Situation i can't really see the downside of creating global variables. Maybe some one could explain it to me (in a basic/easy way since im still a beginner).

标签: pythonpandassqlite

解决方案


推荐阅读