![]() ![]() We can do this with the concat method as follows: These DataFrames have the same shape, so it would be useful to combine them to operate on them together. Imagine we had a second data set with trading information on two additional companies, Facebook (“FB”) and Tesla (“TSLA”). We used sample stock trading data from Amazon (“AMZN”), Apple (“AAPL”) and Google (“GOOG”). In our previous post on Pandas DataFrames, we used sample stock data to show create, explore, and manipulate DataFrames. Now that we understand the difference between vertical combinations with concat and horizontal combinations with merge or join, let’s take a deeper look at how to use these methods. The horizontal combination from a merge operation is similar to a JOIN operator in SQL. Rather, we’re adding columns to existing rows. Notice that in this horizontal combination, we aren’t adding any additional rows. If the two DataFrames have one field in common-such as a stock symbol or company name-you can combine the two DataFrames so that each row contains both the stock trading data and the company background information. The second contains information about the headquarters and numbers of employees for a particular company. The first contains stock trading information various companies. Notice that in a vertical combination with concat, the number of rows has increased but the number of columns has stayed the same.īy contrast, the merge and join methods help to combine DataFrames horizontally. ![]() A vertical combination would use a DataFrame’s concat method to combine the two DataFrames into a single DataFrame with twenty rows. Perhaps the first DataFrame includes 10 rows of stock trading data for one stock while the second DataFrame includes 10 rows of stock trading data for a different stock. Imagine you had two DataFrames with the same columns. The concat method allows you to combine DataFrames vertically. The key distinction is whether you want to combine your DataFrames horizontally or vertically. In this section, we’ll learn when you will want to use one operation over another. ![]() While merge, join, and concat all work to combine multiple DataFrames, they are used for very different things.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |