In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to operate merge in Pandas. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Merge
First of all, let's take a look at the merge operation in dataframe. The merge operation is similar to the join of two tables in a database. Multiple dataframe can be linked through one or more key.
Let's first create two pieces of dataframe data:
Df1 = pd.DataFrame ({'id': [1,2,3,3,5,7,6],' age': range (7)})
Df2 = pd.DataFrame ({'id': [1,2,4,4,5,6,7],' score': range (7)})
We can see that there is an id field in both dataframe. If we want to associate them according to id, we can do it with the pd.merge function:
Although we do not specify which column to complete the association, pandas automatically looks for the same column of two dataframe names to associate. Under normal circumstances, we do not do this, or recommend that you specify the list. Specifying the column name is simple. All we need to do is pass in the parameter on.
If we need to associate with multiple columns, we can also pass in an array. But what if the column names of the two dataframe are not the same? for example, one column of the two dataframe is called id, and the other column is called number. How do you complete the join?
Df1 = pd.DataFrame ({'id': [1,2,3,3,5,7,6],' age': range (7)})
Df2 = pd.DataFrame ({'number': [1,2,4,4,5,6,7],' score': range (7)})
At this point, you need to use left_on to specify the column name of the left table for join, and right_on to specify the column name of the right table for join.
When it comes to join, another issue that has to be raised is the join approach. We all know that there are four common ways of join in database table join operations. They are innner join,left join,right join and outer join. If we take a look at the above results, we will find that there are fewer pieces of data after the association, because the default is inner join, that is, the data that exists in both tables will be retained. If it is left join, all the data on the left side will be retained, and the columns that are not associated will be set to None. Similarly, if it is right join, then all the right table will be retained and all outer join will be retained.
The method of join is controlled by the parameter how. For example, if we want to keep the left table, we can pass in how='left'.
In addition, there are some other parameters for the merge operation, which we will not introduce one by one due to space constraints, so you can consult the relevant documents if you are interested.
Data merging
Another common operation is called data merging. In order to distinguish it from the merge operation, I use Chinese. Although it is also a merger, its logic is different from that of merge. For merge, we need the associated key, which is merged after the data association. The merge operation is a direct merge, row-to-row merge or column-to-column merge, which ignores the merging of data.
This merge operation has been mentioned before in the introduction to numpy, so let's briefly review it here.
First, let's create an array of numpy:
Import numpy as np
Arr = np.random.rand (3,4)
After that, we can use the concatenate function to spell the array horizontally or vertically. The default is vertical spelling:
We can also use the parameter axis to make it spell horizontally:
The same is true for dataframe, but under a different name, concat. If we don't specify it, it will be spliced vertically:
Vertical splicing is aligned by columns, and NaN is populated if the column names do not match.
We can splice it horizontally through the axis parameter:
After reading the above, do you have any further understanding of how to operate merge in Pandas? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.