Pandas provides three main ways to merge data:
- Pandas.merge () method: merging of database styles;
- Pandas.concat () method: An axial connection that stacks multiple objects together along an axis;
- instance Method Combine_first () method: merges overlapping data.
Pandas.merge () method: Merging of database StylesFor example, merging two dataframe with the merge () method: on= ' name ' means to use the Name column as a key; By default, merge does an inner join (inner), which is the intersection of keys. There are other ways to connect left, right, and outer (outer). For example, specify the merge that you just had as a left connection:
Try the external connection again, and the result is the set of the key:
The first three merges are the column names as the connection keys, and Dataframe also has a join () method that can be indexed as a connection key, for example:
Pandas.concat () method: inline joins, that is, to stack multiple objects together along an axisFor example: By default, Concat is connected on the vertical axis (axis=0), which results in a new series. If you want to connect in the horizontal direction, pass in the Axis=1, for example:
instance Method Combine_first () method: Merge overlapping DataFor example: This method is equivalent to: it does a vectorization of the if-else operation, if the S1 in a location of the data is empty, then use S2 the same position on the element to complement, you can be understood as "patching" operation.
The next essay is: Data analysis using Python (13) Pandas: Data conversion, interested friends Welcome to pay attention to this blog, but also welcome you to add comments to discuss.
Data analysis using Python (12) Pandas: Data merge