-1

I am following the Udacity data Science into course and my solution was exactly the one they provided

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")

however it returns me this error:

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']

Can you please help me figure out what's going wrong in my code? thanks in advance!

4

1 回答 1

2

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

q = """
SELECT registrar, enrolment_agency FROM aadhaar_data LIMIT 50;
"""
于 2017-06-10T19:05:37.680 回答