对 Pandas 数据集执行 SQL 查询
我有一个名为df"的 Pandas 数据集.
I have a pandas data set, called 'df'.
我该如何做类似下面的事情;
How can I do something like below;
df.query("select * from df")
谢谢.
对于那些知道R的人来说,有一个叫做sqldf的库,你可以在R中执行SQL代码,我的问题基本上是,python中有没有像sqldf这样的库
For those who know R, there is a library called sqldf where you can execute SQL code in R, my question is basically, is there some library like sqldf in python
推荐答案
这不是什么 pandas.query
应该这样做.您可以查看包 pandasql
(与 R 中的 sqldf
相同)
This is not what pandas.query
is supposed to do. You can look at package pandasql
(same like sqldf
in R )
import pandas as pd
import pandasql as ps
df = pd.DataFrame([[1234, 'Customer A', '123 Street', np.nan],
[1234, 'Customer A', np.nan, '333 Street'],
[1233, 'Customer B', '444 Street', '333 Street'],
[1233, 'Customer B', '444 Street', '666 Street']], columns=
['ID', 'Customer', 'Billing Address', 'Shipping Address'])
q1 = """SELECT ID FROM df """
print(ps.sqldf(q1, locals()))
ID
0 1234
1 1234
2 1233
3 1233
更新 2020-07-10
更新pandasql
ps.sqldf("select * from df")
相关文章