From the course: Python Functions for Data Science

Combine data from pandas objects - Python Tutorial

From the course: Python Functions for Data Science

Start my 1-month free trial

Combine data from pandas objects

- [Narrator] When you work with data using pandas, you may come across situations where you need to combine data from various pandas objects. There are different ways of going about this. I'll be walking through some examples to illustrate pandas functions that are helpful for combining data. Let's say that I have a variable named exam one grades, containing a pandas DataFrame that consists of grades on exam number one for students with student ID numbers one through five. I've displayed it here. And I have another variable named exam two grades, containing a pandas DataFrame that consists of grades, on exam number two, for students with student ID numbers one through five. And I've displayed it here. Now say I want to merge the DataFrame exam one grades, and the DataFrame exam two grades, based on the SID column. With columns from exam one grades appearing on the left, and columns from exam two grades appearing on the right. I can use the merge function from pandas. I'll call merge and pass in exam one grades, and exam two grades, and set the keyword argument, onto the string SID in that order. It would look like this. Note that once I run the cell, a new pandas DataFrame will be returned. Next, say have a variable named SID one to seven, containing a Panda status frame that consists of grades on exam number one, and exam number two, for students with student ID numbers through seven. I've displayed it here. And i have another variable named SID eight to 10, containing a pandas status frame that consists of grades on exam number one, and exam number two, for students with student ID numbers eight through 10. And I've displayed it here. Now say I want to concatenate the DataFrame SID one to seven, and the DataFrame SID eight to 10 along the rows. In other words, I want to concatenate these two DataFrames along access zero. And I want rows from SID one to seven, appearing above rows from SID eight to 10. I can use the concat function from pandas, I'll call concat and pass in a list containing, SID one to seven, and SID eight to 10 in that order, and then set the keyword argument access to zero. It would look like this. Note that once I run the cell, a new pandas DataFrame will be returned. Then say I have a variable named exams one and two, containing a pandas DataFrame that consists of grades on exam number one, and exam number two, for students one through five. I've displayed it here. I haven't they're variable named exam three, containing a pandas DataFrame that consists of grades on exam number three for students one through five. And I've displayed that here. Now say I want to concatenate the DataFrame, exams one and two, and the DataFrame exam three, along the columns. In other words, I want to concatenate these two DataFrames along axis one. And I want columns from exams one and two, appearing to the left of columns from exam three. I can use the concat function from pandas again, this time I'll call concat and passing a list, containing exams one and two and exam three in that order, and then the keyword argument access to one. It will look like this. There we go. Now that you've seen how to use these pandas functions for combining data from pandas objects, feel free to practice using these functions on your data.

Contents