💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
提供了类似于SQL的join接口,供我们进行多表组合。不同的是,pandas可以对index进行join ### 2.3.1. Concatenate 将数据集拼接在一起 ``` # 样本数据 df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'], 'B': ['B0', 'B1', 'B2', 'B3'], 'C': ['C0', 'C1', 'C2', 'C3'], 'D': ['D0', 'D1', 'D2', 'D3']}, index=[0, 1, 2, 3]) df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6', 'A7'], 'B': ['B4', 'B5', 'B6', 'B7'], 'C': ['C4', 'C5', 'C6', 'C7'], 'D': ['D4', 'D5', 'D6', 'D7']}, index=[4, 5, 6, 7]) df3 = pd.DataFrame({'A': ['A8', 'A9', 'A10', 'A11'], 'B': ['B8', 'B9', 'B10', 'B11'], 'C': ['C8', 'C9', 'C10', 'C11'], 'D': ['D8', 'D9', 'D10', 'D11']}, index=[8, 9, 10, 11]) ``` df1 df2 df3如图 ![](https://img.kancloud.cn/2d/0a/2d0a905fec4f39e3e5e06fa7ec80e605_229x466.png) ``` #拼接结果如下 result = pd.concat([df1,df2,df3]) ``` ![](https://img.kancloud.cn/22/bd/22bd82f426ca348cd68a00e1b7439295_233x447.png) ***** ``` df4 = pd.DataFrame({'B': ['B2', 'B3', 'B6', 'B7'], 'D': ['D2', 'D3', 'D6', 'D7'], 'F': ['F2', 'F3', 'F6', 'F7']}, index=[2, 3, 6, 7]) #axis=1进行拼接,两张表横向相同的索引放在同一行。两张表的列并列拼接 pd.concat([df1, df4], axis=1) #结果如下 ``` ![](https://img.kancloud.cn/90/92/90922d643250686dc09e5c91063bfef7_748x230.png) ***** ``` #axis=0,两张表行并列。两张表列名相同的列放在同一列 a =pd.concat([df1, df4], axis=0) ``` ![](https://img.kancloud.cn/65/cc/65cc86817ab5b173b74769f1eab669df_408x531.png) ***** ``` # axis=1,join='inner'。 两张表的列并列放,在行上,只保留 #两张表共有的横向索引行 pd.concat([df1, df4], axis=1, join='inner') ``` ***** ``` # axis=0,join='inner'。 两张表的行并列放,在列上,只保留 #两张表共有列。两张表列名相同的列放在同一列。 b=pd.concat([df1, df4], axis=0, join='inner') b #结果如下图 ``` ![](https://img.kancloud.cn/13/e8/13e8244ec45d8a171349317811ea4e10_405x492.png) ***** ``` # 将两张表按axis=0拼接后。对横向索引从0开始,重排序 d=pd.concat([df1, df4],ignore_index=True,axis=0) d ``` ![](https://img.kancloud.cn/2d/00/2d00fdc65fb68f22bd38d6de99b29efc_229x318.png) ***** ``` # 将两张表按axis=1拼接后。对拼接结果的列从0开始重新命名 d=pd.concat([df1, df4],ignore_index=True,axis=1) d ``` ![](https://img.kancloud.cn/bb/c1/bbc1187831c6a679c2739cb0be0da342_315x243.png) ### 2.3.2. Database-style DataFrame joining/merging **要参照sql的表连接** merge函数用来对两张表进行join,非常类似于sql当中的表联结。 pandas里面不仅可以对columns进行Join,还可以对index进行join。 ~~~python pd.merge(left, right, how='inner', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=True, suffixes=('_x', '_y'), copy=True, indicator=False, validate=None) ~~~ ``` # 基于key列将两张表连接 left = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'], 'A': ['A0', 'A1', 'A2', 'A3'], 'B': ['B0', 'B1', 'B2', 'B3']}) right = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'], 'C': ['C0', 'C1', 'C2', 'C3'], 'D': ['D0', 'D1', 'D2', 'D3']}) #两张表都有"key"列 result = pd.merge(left, right, on='key') #假如两张表要连接的列列名不同,要分别指定基于左表的哪列(left_on='key')和基于右表的哪列(right_on = 'key') pd.merge(left, right, left_on='key',right_on = 'key') ``` ![](https://img.kancloud.cn/ae/1e/ae1ef87ab5c768ab2c5adc70a264761b_743x214.png) ***** **根据多个主键进行join** ``` left = pd.DataFrame({'key1': ['K0', 'K0', 'K1', 'K2'], 'key2': ['K0', 'K1', 'K0', 'K1'], 'A': ['A0', 'A1', 'A2', 'A3'], 'B': ['B0', 'B1', 'B2', 'B3']}) right = pd.DataFrame({'key1': ['K0', 'K1', 'K1', 'K2'], 'key2': ['K0', 'K0', 'K0', 'K0'], 'C': ['C0', 'C1', 'C2', 'C3'], 'D': ['D0', 'D1', 'D2', 'D3']}) #将两张表多列主键都一样的行join,不一样的主键行都舍弃 result = pd.merge(left, right, on=['key1', 'key2']) ``` ![](https://img.kancloud.cn/9f/08/9f080d451df224870cd22e4094399b0b_903x216.png) ***** ``` #连接方式:左连接(how='left')。左边表为基础进行连接,右边表没有对应的行显示为NaN result = pd.merge(left, right, how='left', on=['key1', 'key2'],indicator = True) ``` ![](https://img.kancloud.cn/fd/72/fd722f68ffd44df44b071bd65507410a_909x227.png) ***** ``` #外连接,基于两张表各自的行进行连接,另一张表若没有对应的行值都显示为NaN result = pd.merge(left, right, how='outer', on=['key1', 'key2']) ``` **joining on index** ``` left = pd.DataFrame({'A': ['A0', 'A1', 'A2'], 'B': ['B0', 'B1', 'B2']}, index=['K0', 'K1', 'K2']) right = pd.DataFrame({'C': ['C0', 'C2', 'C3'], 'D': ['D0', 'D2', 'D3']}, index=['K0', 'K2', 'K3']) #以外连接方式连接,根据左边表的全部索引(left_index=true)和右边表的全部索引(right_index=True) result = pd.merge(left, right, left_index=True, right_index=True, how='outer') ``` ![](https://img.kancloud.cn/60/c5/60c597d5946900c75eb11ef0eb273fbb_597x198.png) ***** ``` left = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'], 'B': ['B0', 'B1', 'B2', 'B3'], 'key': ['K0', 'K1', 'K0', 'K1']}) right = pd.DataFrame({'C': ['C0', 'C1'], 'D': ['D0', 'D1']}, index=['K0', 'K1']) #左连接的方式连接两张表。基于左边表的key列(left_on='key')。基于右边表的索引列(right_index=True) result = pd.merge(left, right, left_on='key', right_index=True, how='left'); ``` ![](https://img.kancloud.cn/af/74/af7468d25ab1fc0f17982bb86d6219ce_730x204.png) *****