in Education by
I am trying to create a pandas dataframe that combines all children into one row class Parent(Base): __tablename__ = 'parent' id = Column(Integer, primary_key=True) name = Column(String()) class = Column(String()) all_distance = relationship('Distance', back_populates='parent') all_weight = relationship('Weight', back_populates='parent') class Distance(Base): __tablename__ = 'distance' id = Column(Integer, primary_key=True) distance = Column(String()) finished = Column(String()) parent_id = Column(Integer, ForeignKey('parent.id')) parent = relationship('Parent', back_populates='all_distance') class Weight(Base): __tablename__ = 'weight' id = Column(Integer, primary_key=True) weight = Column(String()) height = Column(String()) parent_id = Column(Integer, ForeignKey('parent.id')) parent = relationship('Parent', back_populates='all_weight') Tables with some data: parent ID | Name | Class 1 | Joe | Paladin 2 | Ron | Mage 3 | Sara | Knight distance ID | distance | finished | parent_id 1 | 2 miles | yes | 1 2 | 3 miles | yes | 1 3 | 1 miles | yes | 1 4 | 10 miles | no | 2 weight ID | Weight | height | parent_id 1 | 5 lbs | 5'3 | 1 2 | 10 lbs | 5'5 | 2 The goal is to create a pandas dataframe that would look like this: 1 | Joe | Paladin | 2 miles | yes | 3 miles | yes | 1 miles | yes | 5lbs | 5'3 2 | Ron | Mage | 10 miles | no | None | None | None | None | 10lbs | 5'5 3 | Sara | Knight | None | None | None | None | None | None | None | None How would I do that? I've gotten somewhat close df = pd.read(db_session.query(Parent, Distance, Weight).join(Distance).join(Weight).statement, db_session.bind) which gives me the data frame of everything joined together. list(df.columns.values) ['id', 'name', 'class', 'id', 'distance', 'finished', 'id', 'weight', 'height'] How do I prevent the same column headers? ie - id is now 3 times However when I try to make a pivot table: df.pivot(index="id") it is returning an error: Traceback (most recent call last): File "", line 1, in File "/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 5194, in pivot return pivot(self, index=index, columns=columns, values=values) File "/anaconda2/lib/python2.7/site-packages/pandas/core/reshape/reshape.py", line 400, in pivot indexed = self.set_index(cols, append=append) File "/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 3909, in set_index level = frame[col]._values File "/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 2688, in __getitem__ return self._getitem_column(key) File "/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 2698, in _getitem_column result = self._constructor(self._data.get(key)) File "/anaconda2/lib/python2.7/site-packages/pandas/core/internals.py", line 4130, in get raise TypeError("cannot label index with a null key") TypeError: cannot label index with a null key JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
you are trying to pass 'id' as index so the pivot fails. It should be: df.pivot(df.index,"id")

Related questions

0 votes
    I have a simple method to search a pandas dataframe column for a list of keywords; however, I'd like to create a ... do everyth 28,passei o dia com o meu amor comemo demai...
asked Apr 13, 2022 in Education by JackTerrance
0 votes
    I have a pandas series containing a list of dictionaries. I'd like to parse the contents of the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    Currently I'm returning column name of the max value in the each row. df['Active'] = df.idxmax( ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    Currently I'm returning column name of the max value in the each row. df['Active'] = df.idxmax( ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    18F-AV-1451-A07 Value refer to another sheet called "CONTENT" in which column "B" and row "3". ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have a csv file and I am reading this file in python using pandas. I want to read each row of ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I have a dataset with employee payroll information (df2). It has a date, job title, shift start ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    Say I have a dictionary that the key of year, and the corresponding value of a list of values. Is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    In the code below I pivot a dataframe using an index date. After the pivot, I need to get the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    I have a blade template that creates a form. The form uses a foreach loop to iterate through a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I have a blade template that creates a form. The form uses a foreach loop to iterate through a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    I'm starting from the pandas DataFrame docs here: http://pandas.pydata.org/pandas-docs/stable/dsintro.html I'd ... =1)] print valdict Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    How do I check whether data in a query exists? For example: users_query = User.query.filter_by(email=' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I am using Python 2.6.6 and SQLAlchemy 0.6.6 to handle a one to many relationship in my database ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    Hi I am having problems with relation and database design. There are three tables called 'articles' , ' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
...