针对 pandas 数据帧执行 SQL 命令时出错

Error executing SQL command against pandas dataframe

我正在学习 Udacity 数据科学课程,我的解决方案正是他们提供的解决方案

import pandas
import pandasql

def select_first_50(filename):
    # Read in our aadhaar_data csv to a pandas dataframe.  Afterwards, we rename the columns
    # by replacing spaces with underscores and setting all characters to lowercase, so the
    # column names more closely resemble columns names one might find in a table.
    aadhaar_data = pandas.read_csv(filename)
    aadhaar_data.rename(columns = lambda x: x.replace(' ', '_').lower(), inplace=True)

    # Select out the first 50 values for "registrar" and "enrolment_agency"
    # in the aadhaar_data table using SQL syntax. 
    #
    # Note that "enrolment_agency" is spelled with one l. Also, the order
    # of the select does matter. Make sure you select registrar then enrolment agency
    # in your query.
    #
    # You can download a copy of the aadhaar data that we are passing 
    # into this exercise below:
    # https://s3.amazonaws.com/content.udacity-data.com/courses/ud359/aadhaar_data.csv
    q = """
    SELECT registrar, enrolment_agency FROM aadhar_data LIMIT 50;
    """

    #Execute your SQL command against the pandas frame
    aadhaar_solution = pandasql.sqldf(q.lower(), locals())
    return aadhaar_solution 

print select_first_50("/home/trina/Documents/Udacity_datascience/aadhaar_data.csv")

然而 returns 我这个错误:

File "pandas_sql.py", line 29, in <module>
    print select_first_50("/home/trina/Documents/Udacity_datascience/aadhaar_data.csv")
  File "pandas_sql.py", line 26, in select_first_50
    aadhaar_solution = pandasql.sqldf(q.lower(), locals())
  File "/home/trina/anaconda2/lib/python2.7/site-packages/pandasql/sqldf.py", line 156, in sqldf
    return PandaSQL(db_uri)(query, env)
  File "/home/trina/anaconda2/lib/python2.7/site-packages/pandasql/sqldf.py", line 63, in __call__
    raise PandaSQLException(ex)
pandasql.sqldf.PandaSQLException: (sqlite3.OperationalError) no such table: aadhar_data [SQL: '\n\tselect registrar, enrolment_agency from aadhar_data limit 50;\n\t']

你能帮我弄清楚我的代码出了什么问题吗? 提前致谢!

看起来只是打字错误:在 SQL 查询中尝试使用 aadhaar_data 作为 table 名称而不是 aadhar_data

q = """
SELECT registrar, enrolment_agency FROM aadhaar_data LIMIT 50;
"""