phpbb能embedding不少东西呢
pandas和sql各自擅长的领域
版主: hci
#23 Re: pandas和sql各自擅长的领域
pandas AND SQL是data scientist最基本的两个基本技能了。pandas做EDA方便,是前期的好用工具,BA和DS的基本技能。 到最后deploy数据大的的sql和pyspark。
#24 Re: pandas和sql各自擅长的领域
wildthing 写了: 2025年 10月 18日 21:58in memory DB is still DB. The basic client/server architecture does not change just because the server is not in memory.
DB uses data structures like B-Tree to organize indexed tables to allow fast look up.
DB also has other properties that are important for fault tolerance. ACID properties, for example, are not available in Pandas.
Transaction management is also important for massive parallel services.DB is huge area. Don't underestimate its importance.
有道理,我只知道pandas和numpy經常一起用,跟DL、AI和GPU強相關。SQL跟互聯網和transaction和CPU強相關,跟深度學習大模型沒什麼關係。
#25 Re: pandas和sql各自擅长的领域
pnlmpnlm 写了: 2025年 10月 19日 02:34pandas AND SQL是data scientist最基本的两个基本技能了。pandas做EDA方便,是前期的好用工具,BA和DS的基本技能。 到最后deploy数据大的的sql和pyspark。
這些都跟LLM關係很遠,pandas勉強還能做一點預處理。
#26 Re: pandas和sql各自擅长的领域
duckdb就得了
无论是熊猫还是狗熊, 都需要学习作者草创的带bug的语言, 跟久经考验的sql根本没法比
用了duckdb, 你都不用绑定python
代码: 全选
using DuckDB
using DataFrames
# 1. Create a DuckDB database connection
# You can connect to an in-memory database or a persistent file
db = DuckDB.DB() # In-memory database
# db = DuckDB.DB("my_database.duckdb") # Persistent database file
conn = DBInterface.connect(db)
# 2. Create a table
DBInterface.execute(conn, "CREATE TABLE users (id INTEGER, name VARCHAR, age INTEGER)")
# 3. Insert data
DBInterface.execute(conn, "INSERT INTO users VALUES (1, 'Alice', 30)")
DBInterface.execute(conn, "INSERT INTO users VALUES (2, 'Bob', 25)")
DBInterface.execute(conn, "INSERT INTO users VALUES (3, 'Charlie', 35)")
# Using prepared statements for inserting multiple rows
stmt = DBInterface.prepare(conn, "INSERT INTO users VALUES (?, ?, ?)")
DBInterface.execute(stmt, (4, "David", 40))
DBInterface.execute(stmt, (5, "Eve", 28))
# 4. Query data
results_df = DBInterface.execute(conn, "SELECT * FROM users WHERE age > 30") |> DataFrame
println("Users older than 30:")
println(results_df)
# Aggregate query
avg_age_df = DBInterface.execute(conn, "SELECT AVG(age) AS average_age FROM users") |> DataFrame
println("\nAverage age of users:")
println(avg_age_df)
# 5. Close the connection
DBInterface.close(conn)TheMatrix 写了: 2025年 10月 18日 10:40知乎这篇讲pandas和sql各自擅长的领域。pandas我不是特别熟,不过我看就下面这段pandas代码,完全用sql也是很容易的。
https://www.zhihu.com/question/40269042 ... 1877217409
代码: 全选
import pandas as pd from sqlalchemy import create_engine # 假设你已经配置好了数据库连接 db_connection_str = 'mysql+pymysql://user:password@host/dbname' db_connection = create_engine(db_connection_str) # 读取SQL查询的结果到DataFrame # 注意,这里是读取查询结果,不是全表! sql_query = """ SELECT product_id, product_name, quantity, price, order_time FROM orders WHERE order_time >= '2023-10-01' AND order_time < '2023-11-01' AND category = 'A'; """ df = pd.read_sql(sql_query, con=db_connection) # --- 现在进入Pandas的主场 --- # 1. 数据清洗(假设有缺失值) df.dropna(inplace=True) # 2. 计算每个订单的销售额 df['sales'] = df['quantity'] * df['price'] # 3. 按产品ID分组,计算总销售额和总销量,找出爆款 product_summary = df.groupby('product_id').agg( total_sales=('sales', 'sum'), total_quantity=('quantity', 'sum'), product_name=('product_name', 'first') # 顺便把名字带出来 ).sort_values(by='total_sales', ascending=False) print("--- 销售额 Top 10 爆款 ---") print(product_summary.head(10)) # 4. 分析周内销售趋势,看看周末是不是卖得好 df['order_time'] = pd.to_datetime(df['order_time']) df['weekday'] = df['order_time'].dt.dayofweek # 0=周一, 6=周日 weekday_sales = df.groupby('weekday')['sales'].sum() print("\n--- 周内销售分布 ---") print(weekday_sales) # 5. 接下来你还可以做可视化、跑关联规则、做价格敏感度分析…… # 这些复杂的、探索性的操作,用SQL写会非常非常痛苦,但在Pandas里就是几行代码的事。
工具机谈智商, 犹如妓女谈贞操, 哪壶不开提哪壶
呼叫鸡谈造谣, 犹如站街女谈卖淫, 那是行家里手
#27 Re: pandas和sql各自擅长的领域
这大哥写了个啥啊。这问题也问的是个啥。
TheMatrix 写了: 2025年 10月 18日 10:40知乎这篇讲pandas和sql各自擅长的领域。pandas我不是特别熟,不过我看就下面这段pandas代码,完全用sql也是很容易的。
https://www.zhihu.com/question/40269042 ... 1877217409
代码: 全选
import pandas as pd from sqlalchemy import create_engine # 假设你已经配置好了数据库连接 db_connection_str = 'mysql+pymysql://user:password@host/dbname' db_connection = create_engine(db_connection_str) # 读取SQL查询的结果到DataFrame # 注意,这里是读取查询结果,不是全表! sql_query = """ SELECT product_id, product_name, quantity, price, order_time FROM orders WHERE order_time >= '2023-10-01' AND order_time < '2023-11-01' AND category = 'A'; """ df = pd.read_sql(sql_query, con=db_connection) # --- 现在进入Pandas的主场 --- # 1. 数据清洗(假设有缺失值) df.dropna(inplace=True) # 2. 计算每个订单的销售额 df['sales'] = df['quantity'] * df['price'] # 3. 按产品ID分组,计算总销售额和总销量,找出爆款 product_summary = df.groupby('product_id').agg( total_sales=('sales', 'sum'), total_quantity=('quantity', 'sum'), product_name=('product_name', 'first') # 顺便把名字带出来 ).sort_values(by='total_sales', ascending=False) print("--- 销售额 Top 10 爆款 ---") print(product_summary.head(10)) # 4. 分析周内销售趋势,看看周末是不是卖得好 df['order_time'] = pd.to_datetime(df['order_time']) df['weekday'] = df['order_time'].dt.dayofweek # 0=周一, 6=周日 weekday_sales = df.groupby('weekday')['sales'].sum() print("\n--- 周内销售分布 ---") print(weekday_sales) # 5. 接下来你还可以做可视化、跑关联规则、做价格敏感度分析…… # 这些复杂的、探索性的操作,用SQL写会非常非常痛苦,但在Pandas里就是几行代码的事。
#28 Re: pandas和sql各自擅长的领域
TheMatrix 写了: 2025年 10月 18日 22:00“搞统计分析的好多是程序盲”,你是说他们不会写sql?还是不会写pandas?
pandas我也学了几次,每次都感觉和sql功能是重复的,所以我还是用的sql。
程序盲不是说不会写程序,而是不理解这些程序的优劣性。出了问题无法解决。外行的程序质量非常底下。这是现在的软件如此糟糕的原因。
#29 Re: pandas和sql各自擅长的领域
SQL的中间过程如果出错,很难修改。是个开发效率低下的方式。不能说是个程序语言。Pandas和R的dataframe要好用的多。
wokao 写了: 2025年 10月 19日 07:41duckdb就得了
无论是熊猫还是狗熊, 都需要学习作者草创的带bug的语言, 跟久经考验的sql根本没法比
用了duckdb, 你都不用绑定python
代码: 全选
using DuckDB using DataFrames # 1. Create a DuckDB database connection # You can connect to an in-memory database or a persistent file db = DuckDB.DB() # In-memory database # db = DuckDB.DB("my_database.duckdb") # Persistent database file conn = DBInterface.connect(db) # 2. Create a table DBInterface.execute(conn, "CREATE TABLE users (id INTEGER, name VARCHAR, age INTEGER)") # 3. Insert data DBInterface.execute(conn, "INSERT INTO users VALUES (1, 'Alice', 30)") DBInterface.execute(conn, "INSERT INTO users VALUES (2, 'Bob', 25)") DBInterface.execute(conn, "INSERT INTO users VALUES (3, 'Charlie', 35)") # Using prepared statements for inserting multiple rows stmt = DBInterface.prepare(conn, "INSERT INTO users VALUES (?, ?, ?)") DBInterface.execute(stmt, (4, "David", 40)) DBInterface.execute(stmt, (5, "Eve", 28)) # 4. Query data results_df = DBInterface.execute(conn, "SELECT * FROM users WHERE age > 30") |> DataFrame println("Users older than 30:") println(results_df) # Aggregate query avg_age_df = DBInterface.execute(conn, "SELECT AVG(age) AS average_age FROM users") |> DataFrame println("\nAverage age of users:") println(avg_age_df) # 5. Close the connection DBInterface.close(conn)
#30 Re: pandas和sql各自擅长的领域
magagop 写了: 2025年 10月 19日 04:50有道理,我只知道pandas和numpy經常一起用,跟DL、AI和GPU強相關。SQL跟互聯網和transaction和CPU強相關,跟深度學習大模型沒什麼關係。
pandas是用numpy实现的。
这个是做数据预处理的。和ML没有一毛钱关系。
-
TheMatrix楼主
- 论坛支柱

2024年度优秀版主
TheMatrix 的博客 - 帖子互动: 283
- 帖子: 13768
- 注册时间: 2022年 7月 26日 00:35
#31 Re: pandas和sql各自擅长的领域
cernivtsi 写了: 2025年 10月 18日 22:10正确的,越来越没必要学pandas
用嵌入式DB,如DuckDB,已可替代pandas,不但适于快速原型 + 数据探索,在大数据 + 高性能分析更胜一筹
SQL主要的问题是和python接口不方便。
#32 Re: pandas和sql各自擅长的领域
impedance mismatch.
OO doesn't work well with relational data.
This is a forever problem.
#33 Re: pandas和sql各自擅长的领域
还有一种办法就是不用pandas这类轮子,直接语言(sql)调用语言(c#,java etc),如:用c#写好函数编译成dll,然后sql server 直接call,oracle也可以这么干,直接call Java写的函数,
这样就扩招了SQL的不方便性(如果SQL里能调用程序设计语言,实际上就自由了),同时也可以保证正常使用DB的特性如:transaction什么的。
-
TheMatrix楼主
- 论坛支柱

2024年度优秀版主
TheMatrix 的博客 - 帖子互动: 283
- 帖子: 13768
- 注册时间: 2022年 7月 26日 00:35
#34 Re: pandas和sql各自擅长的领域
cangyoujiacuo 写了: 2025年 10月 19日 10:38还有一种办法就是不用pandas这类轮子,直接语言(sql)调用语言(c#,java etc),如:用c#写好函数编译成dll,然后sql server 直接call,oracle也可以这么干,直接call Java写的函数,
这样就扩招了SQL的不方便性(如果SQL里能调用程序设计语言,实际上就自由了),同时也可以保证正常使用DB的特性如:transaction什么的。
嗯。这个很好。好像Snowflake和SparkSQL都可以这样干吧?
-
TheMatrix楼主
- 论坛支柱

2024年度优秀版主
TheMatrix 的博客 - 帖子互动: 283
- 帖子: 13768
- 注册时间: 2022年 7月 26日 00:35
#35 Re: pandas和sql各自擅长的领域
cangyoujiacuo 写了: 2025年 10月 19日 10:38还有一种办法就是不用pandas这类轮子,直接语言(sql)调用语言(c#,java etc),如:用c#写好函数编译成dll,然后sql server 直接call,oracle也可以这么干,直接call Java写的函数,
这样就扩招了SQL的不方便性(如果SQL里能调用程序设计语言,实际上就自由了),同时也可以保证正常使用DB的特性如:transaction什么的。
不过这主要解决的是SQL aggregate函数的问题。而程序的总体结构,用SQL写还是不如用python写方便。
#37 Re: pandas和sql各自擅长的领域
dataframe除非也有中间的dataframe, 否则一样
而duckdb每次调用sql, 都可以对应一个dataframe, 是可以调试的
所以唯一区别就是交流的语言, 一个是草创的, 一个是sql
工具机谈智商, 犹如妓女谈贞操, 哪壶不开提哪壶
呼叫鸡谈造谣, 犹如站街女谈卖淫, 那是行家里手
#38 Re: pandas和sql各自擅长的领域
raw data用pandas更快,multiple process分块读,5gb csv 2秒读完,秒杀sql,duck都慢。sql已死,有事烧纸
#39 Re: pandas和sql各自擅长的领域
cangyoujiacuo 写了: 2025年 10月 19日 10:38还有一种办法就是不用pandas这类轮子,直接语言(sql)调用语言(c#,java etc),如:用c#写好函数编译成dll,然后sql server 直接call,oracle也可以这么干,直接call Java写的函数,
这样就扩招了SQL的不方便性(如果SQL里能调用程序设计语言,实际上就自由了),同时也可以保证正常使用DB的特性如:transaction什么的。
你这是不用造好的轮子,非要自己再造。。。pandas是c写的,可比c#,java快多了
#40 Re: pandas和sql各自擅长的领域
anesthetic 写了: 2025年 10月 19日 13:08raw data用pandas更快,multiple process分块读,5gb csv 2秒读完,秒杀sql,duck都慢。sql已死,有事烧纸
Raw data 在database里怎么读csv.






