r/pythonhelp • u/Grubersteindeville • Jul 14 '22
Databases and Tables
Hi all, I am a newbie Python user, and I'm dumb as hell. I can't figure out how to figure out the assignment, which I will post here:
Read in Student_Grade_Prediction2.csv as Students_df and create a database called Students.
Remember that you also need to create a cursor to execute the commands below.Select all the students who scored less than or equal to 12 on the first exam (G1) ordered in
descending order by the first exam scores.
As you can see, I am supposed to read in a csv file, create a database, and then a table with a cursor? I don't know really, this is where I get lost. Here is my code:
import sqlite3
con = sqlite3.connect('Students_df')
cursor = conn.cursor()
import pandas as pd
Students = pd.read_csv(
'Student_Grade_Prediction2.csv')
Students.to_sql("Student_records", con, if_exists = "replace")
cursor.execute("CREATE TABLE IF NOT EXISTS students (ID INT PRIMARY KEY, school TEXT, sex TEXT, age INT, address TEXT, famsize TEXT, Pstatus TEXT, Medu INT, Fedu INT, Mjob TEXT, Fjob TEXT, reason TEXT, guardian TEXT, traveltime INT, studytime INT, failures INT, schoolsup TEXT, famsup TEXT, paid TEXT, activities TEXT, nursery TEXT, higher TEXT, internet TEXT, romantic TEXT, famrel INT, freetime INT, goout INT, Dalc INT, Walc INT, health INT, absences INT, G1 INT, G2 INT, G3 INT;))"
I'm getting an "unexpected EOF while parsing" error
Sorry if this is against rules, I just really need help. Thanks in advance
1
1
1
u/CraigAT Jul 14 '22
You appear to have the database name and the dataframe name mixed up - I would expect the dataframe to be the one ending _df.