对pandas datafram执行SQL命令时出错

2024-04-29 13:58:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习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")

但是它会返回以下错误:

^{pr2}$

你能帮我找出我的代码出了什么问题吗? 提前谢谢!在


Tags: columnscsvtheinimportpandasdataselect