In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the Python data merge concat function and merge function how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Python data merge concat function and merge function how to use the article will have a harvest, let's take a look at it.
1. Concat function
The 1.concat () function can stack multiple objects along an axis in a way similar to merging data tables in a database.
Pandas.concat (objs, axis=0, join='outer', join_axes=None, ignore_index=False, keys=None, levels=None, verify_integrity=False, sort=None, copy=True)
two。 The meaning of the parameter is as follows:
Parameter action axis indicates the axial direction of the connection, which can be 0 or 1. By default, it defaults to 0join for connection, inner for inner connection, and outer for outer connection. By default, Boolean values are received using outer connection ignore_index. Default is False. If set to True, it clears the existing index and resets the index value keys receive sequence, indicating that the outermost index levels is added to the specific level (unique value) used to build the MultiIndex. After names sets the keys and level parameters, the name used to create the hierarchical level verify_integerity checks whether the new connection axis contains duplicates. Receive a Boolean value. When set to True, an error will be thrown if there is a duplicate axis. The default is False.
3. According to the direction of the axis, the stack can be divided into horizontal stacking and vertical stacking. The default is vertical stacking.
4. When stacking data, the default method is external connection (join parameter is set to outer), of course, it can also be set to inner connection through join=inner.
1) horizontal stacking and external connection import pandas as pddf1=pd.DataFrame ({'A', ['A0','A','A','A': ['B0','B': ['B0','B','B']]}) df1
Horizontal stack merges df1 and df2, using external connection
Pd.concat ([df1,df2], join='outer',axis=1)
2) Vertical stacking and inner link import pandas as pdfirst=pd.DataFrame ({'A': ['A0','A1', 'A2'],' Bean: ['B0','B0', B1,'B2'],'C0: ['C0', C1', C2']}) first
Second=pd.DataFrame ({'Baking: [' B3BZ], 'C5': [' C3BZ], 'C5': [' C3BZ], 'DBZ: [' D3BZ]}) second
3. When merging using the concat () function, if the value of the axis parameter is set to 0 and the value of the join parameter is set to inner, it represents merging using vertical stacking and inner connection.
Pd.concat ([first,second], join='inner',axis=0)
Second, merge () function
1) Primary key merges data
When using the merge () function to merge, the overlapping column index is used as the merge key by default, and the data is merged by inner join, that is, the overlapping part of the row index is taken.
Import pandas as pdleft=pd.DataFrame ({'key':, [' KOBZ], 'AZL: [' AOBZ: ['B0', ['B0','B0']]}) left
Right=pd.DataFrame ({'key':, [' K0','K1','K2','K3'],'C0: ['C0', C1', C2', C3'],'D3: ['D0', D1, D2, D3]}) right
Pd.merge (left,right,on='key')
2) the merge () function also supports merging DataFrame objects with multiple overlapping columns.
Import pandas as pddata1=pd.DataFrame ({'key':, [' KOBZ], 'AZL: [' AOBZ: ['B0', ['B0','B0']]}) data1
Data2=pd.DataFrame ({'key':, [' K0','K0,', of, 'D3']}) data2
Pd.merge (data1,data2,on= ['key','B'])
1) merge data according to row index
The join () method can join multiple DataFrame objects by indexing or specifying columns.
Join (other,on = None,how = 'left',lsuffix ='', rsuffix ='', sort = False)
The parameter acts as the on name, which is used to connect the column name how . You can choose any one of {'left'',' right'',''outer'',' 'inner''}. The left concatenation method is used by default. Sort sorts the merged data according to the connection key. The default is Falseimport pandas as pddata3=pd.DataFrame ({'A': ['A0','A','A','A': ['B0','B': ['B0','B1, and 'B2']}) data3
Data4=pd.DataFrame ({'Cure: [' C0,'C1, 'C2'],' Dao: ['D0,'D1, 'D2']}, index= [' axiomagronomy, c']) data3.join (data4,how='outer') # external connection
Data3.join (data4,how='left') # left connection
Data3.join (data4,how='right') # right connection
Data3.join (data4,how='inner') # internal connection
Import pandas as pdleft = pd.DataFrame ({'Aids: [' A0,'A1,'A2], 'Bao: [' B0,'B1,'B2], 'key': [' K0,'K1,'K2]}) left
Right = pd.DataFrame ({'Crune: [' C0,'C1, 'C2'],' Dao: ['D0BZ,' D1BZ]]}, index= ['K0BZ,' K1BZ]) right
The on parameter specifies the column name of the connection
The left.join (right,how='left',on='key') # on parameter specifies the column name of the connection
2) merge overlapping data
When missing data occurs in the DataFrame object, and we want to populate the missing data with data from other DataFrame objects, we can populate the missing data with the combine_first () method.
Import pandas as pdimport numpy as npfrom numpy import NANleft = pd.DataFrame ({'Aids: [np.nan,' A1,'A2,'A3], 'Bones: [np.nan,' B1, np.nan,'B3], 'key': [' K0,'K1, K2, K3]}) left
Right = pd.DataFrame ({'Aguilar: [' C _ 0,'C _ 1]],'B: ['D _ 0,'D _ 1]]}, index= [1m _ 0 ~ 2]) right
Fill in the missing parts of left with right data
Left.combine_first (right) # fill the missing parts of left with right data
This is the end of the article on "how to use the concat function and merge function of Python data merging". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use the concat function and merge function of Python data merging". If you want to learn more, you are welcome to follow the industry information channel.
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.