In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use the merge function in pandas. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Merge
The merge function method is similar to join in SQL and can be pd.merge or df.merge, except that the data to be merged in the latter is
Pd.merge (left: 'DataFrame | Series', right:' DataFrame | Series', how: 'str' =' inner', on: 'IndexLabel | None' = None, left_on:' IndexLabel | None' = None, right_on: 'IndexLabel | None' = None, left_index:' bool' = False, right_index: 'bool' = False, sort:' bool' = False Suffixes: 'Suffixes' = (' _ x','_ y'), copy: 'bool' = True, indicator:' bool' = False, validate: 'str | None' = None,)->' DataFrame'
In the function method, the key parameters have the following meanings:
Left: left side data for connection
Right: data on the right side of the connection
How: data connection method. Default is inner. Optional outer, left and right
On: connect key fields, both in the left and right data need to exist, otherwise use left_on and right_on
Left_on: the key field of the data on the left for the connection
Right_on: the key field of the data on the right for the connection
Left_index: True indicates that the left index is a join key field
Right_index: True indicates that the right index is a join key field
Suffixes: 'Suffixes' = (' _ x _ names,'_ y'), which can be freely specified, that is, the column names will display the suffix after merging the same column names.
Indicator: whether to show the home source of a row of data after merging
Next, we will demonstrate the function of this function.
Basic merger
In [55]: df1 = pd.DataFrame ({'key': [' foo', 'bar',' bal'],...: 'value2': [1,2,3]}) In [56]: df2 = pd.DataFrame ({' key': ['foo',' bar', 'baz'],.:' value1': [5,6] 7]}) In [57]: df1.merge (df2) Out [57]: key value2 value10 foo 1 51 bar 2 6
Other connection methods
In [58]: df1.merge (df2, how='left') Out [58]: key value2 value10 foo 1 5.01 bar 2 6.02 bal 3 NaNIn [59]: df1.merge (df2, how='right') Out [59]: key value2 value10 foo 1.051 bar 2.0 62 baz NaN 7In [60]: df1.merge (df2) How='outer') Out [60]: key value2 value10 foo 1.05.01 bar 2.06.02 bal 3.0 NaN3 baz NaN 7.0In [61]: df1.merge (df2 How='cross') Out [61]: key_x value2 key_y value10 foo 1 foo 51 foo 1 bar 62 foo 1 baz 73 bar 2 foo 54 bar 2 bar 65 bar 2 baz 76 bal 3 foo 57 bal 3 bar 68 bal 3 baz 7
Specify connection key
You can specify a single connection key or multiple connection keys
In [62]: df1 = pd.DataFrame ({'lkey1': [' foo', 'bar',' bal'],...: 'lkey2': [' await, 'baked,' c'],...: 'value2': [1,2,3]}) In [63]: df2 = pd.DataFrame ({' rkey1': ['foo',' bar') 'baz'],...:' rkey2': ['await,' baked,'c'],...: 'value2': [5,6 7]}) In [64]: df1Out [64]: lkey1 lkey2 value20 foo a 11 bar b 22 bal c 3In [65]: df2Out [65]: rkey1 rkey2 value20 foo a 51 bar b 62 baz c 7In [66]: df1.merge (df2, left_on='lkey1') Right_on='rkey1') Out [66]: lkey1 lkey2 value2_x rkey1 rkey2 value2_y0 foo a 1 foo a 51 bar b 2 bar b 6In [67]: df1.merge (df2, left_on= ['lkey1','lkey2'], right_on= [' rkey1'] 'rkey2']) Out [67]: lkey1 lkey2 value2_x rkey1 rkey2 value2_y0 foo a 1 foo a 51 bar b 2 bar b 6
Specify the index as the key
Out [68]: df1.merge (df2, left_index=True, right_index=True) Out [68]: lkey1 lkey2 value2_x rkey1 rkey2 value2_y0 foo a 1 foo a 51 bar b 2 bar b 62 bal c 3 baz c 7
Set duplicate column suffix
In [69]: df1.merge (df2, left_on='lkey1', right_on='rkey1', suffixes= ['left', 'right']) Out [69]: lkey1 lkey2 value2 left rkey1 rkey2 value2 right 0 foo a 1 foo a 51 bar b 2 bar b 6
Connection indication
Add a new column to display data sources
In [70]: df1.merge (df2, left_on='lkey1', right_on='rkey1', suffixes= ['left', 'right'], how='outer' ...: indicator=True...:) Out [70]: lkey1 lkey2 value2 left rkey1 rkey2 value2 right _ merge0 foo a 1.0 foo a 5.0 both1 bar b 2.0 bar b 6.0 both2 bal c 3.0 NaN left_only3 NaN baz C 7.0 right_only Thank you for your reading! This is the end of the article on "how to use the merge function in pandas". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.