python - 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).
解决方案
推荐阅读
- google-sheets - 在 HLOOKUP 中搜索组合范围时遇到问题
- c# - 将 'var' 与 'is' 一起使用会导致 Resharper 说代码在启发式上无法访问
- python - 停止 asyncio.run() 忽略我的代码的剩余部分
- node.js - (node:14) UnhandledPromiseRejectionWarning: ReferenceError: ms is not defined
- java - 如何通过 debezium CDC 机制反序列化从 kafka 代理收到的 BigDecimal 值?
- opencv - 将 GpuMat 直接传递给 cufftExecC2C 函数以进行快速傅立叶变换
- javascript - 如何使用响应对象上的 state() 方法在路由处理程序中设置 cookie?
- php - YouTube Feed 有一个名为 @attributes 的对象,我该如何访问它?
- javascript - 为什么我不能使用 array.indexOf(this.innerHTML)?
- java - 如何每5秒执行一次netty(服务器)