数据集上执行SQL查询

数据集上执行SQL查询

本文介绍了在 pandas 数据集上执行SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为"df"的熊猫数据集.

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 pandas.query 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

这篇关于在 pandas 数据集上执行SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 20:30