AZhao
AZhao

Reputation: 14435

Duplicate columns when querying SQLAlchemy into Pandas DF?

I'm building a python data library for analysis on top of a star schema database and am having trouble integrating pandas and sqlalchemy because of some duplicate column keys in the data frame.

Here's the classes:

class Student(Base):
    __tablename__ = 'DimStudent'

    id = Column('StudentKey', Integer, primary_key=True)
    srcstudentid = ('SrcStudentId', Integer)
    firstname = Column('FirstName', String)
    middlename = Column('MiddleName', String)
    lastname = Column('LastName', String)
    lep = Column('LimitedEnglishProficiency', String)
    frl = Column('FreeReducedLunch', String)
    sped = Column('SpecialEducation', String)

class School(Base):
    __tablename__ = 'DimSchool'

    id = Column('SchoolKey', Integer, primary_key=True)
    name = Column('SchoolName', String)
    district = Column('SchoolDistrict', String)
    statecode = Column('StateCode', String)

class StudentScore(Base):
    __tablename__ = 'FactStudentScore'

    studentkey = Column('StudentKey', Integer, ForeignKey('DimStudent.StudentKey'), primary_key=True)
    teacherkey = Column('TeacherKey', Integer, ForeignKey('DimTeacher.TeacherKey'), primary_key=True)    
    schoolkey = Column('SchoolKey', Integer, ForeignKey('DimSchool.SchoolKey'), primary_key = True)
    assessmentkey = Column('AssessmentKey', Integer, ForeignKey('DimAssessment.AssessmentKey'), primary_key=True)
    subjectkey = Column('SubjectKey', Integer, ForeignKey('DimSubject.SubjectKey'), primary_key=True)
    yearcyclekey = Column('YearCycleKey', Integer, ForeignKey('DimYearCycle.YearCycleKey'), primary_key=True)
    pointspossible = Column('PointsPossible', Integer)
    pointsreceived = Column('PointsReceived', Integer)

    student = relationship("Student", backref=backref('studentscore'))
    school = relationship("School", backref=backref('studentscore'))
    assessment = relationship("Assessment", backref='studentscore')
    teacher = relationship("Teacher", backref='studentscore')
    subject = relationship("Subject", backref='studentscore')
    yearcycle = relationship("YearCycle", backref='studentscore')    

Whenever I query my data, I consistently come up with duplicate columns of data, for example, the school key in this ORM call and then build a dataframe from it.

school = session.query(StudentScore, School, Subject)\    
.join(StudentScore.school).join(StudentScore.subject)\
.filter(School.name.like('%Dever%'))\
.filter(Subject.code == 'Math')

 a = pd.read_sql(school.statement, school.session.bind)

This SO thread provides a nice transpose technique to remove the duplicate.

 a = a.T.drop_duplicates().T

However, I'm still running into an error when I interact with this dataframe within the IDE variable explorer. The error is: "Reindexing only valid with uniquely valued Index objects"

Any idea where the issue is?

Upvotes: 2

Views: 1945

Answers (1)

AZhao
AZhao

Reputation: 14435

Found the correct answer! Instead of the most simple:

 a = a.T.drop_duplicates().T

I instead used a groupby to remove the duplicates:

df.T.groupby(level=0).first().T

That said, I'm not sure the drivers of my original error were. Also the new line of code works 10-100x faster than the old one.

Upvotes: 1

Related Questions