文档详情

Python数据分析基础与应用电子活页5-14使用join()函数通过索引或指定列合并数据集的数据.docx

发布:2025-05-25约1.3千字共2页下载文档
文本预览下载声明

Python数据分析基础与应用

模块

PAGE2

PAGE3

电子活页5-14使用join()函数通过索引或指定列合并数据集的数据

【技能训练5-15】使用join()函数通过索引或指定列合并数据集的数据

【训练要求】

在JupyterNotebook开发环境中创建j5-15.ipynb,然后编写代码使用join()函数通过索引或指定列合并数据集的数据。

【实施过程】

(1)join()函数使用默认连接方式合并数据

代码如下:

importpandasaspd

data1={Name:[路远,温暖,向北,阳光,白雪,夏天,云朵],

Sex:[男,男,女,男,女,男,女],

Age:[20,19,22,23,18,20,22]}

label1=[stu01,stu02,stu03,stu04,stu05,stu06,stu07]

df_left1=pd.DataFrame(data1,index=label1)

data2={Id:[1002,1003,1006,1007,1008,1001],

Score:[80,89,84,78,92.5,71]}

label2=[stu08,stu09,stu10,stu11,stu12,stu13]

df_right1=pd.DataFrame(data2,index=label2)

df_left1.join(df_right1)

输出结果:

(2)join()函数使用外连接方式合并数据

代码如下:

df_left1.join(df_right1,how=outer,sort=True)

输出结果:

(3)join()函数使用参数on指定重叠的列名合并数据

代码如下:

importpandasaspd

data1={Id:[1002,1003,1004,1005,1006,1007,1008],

Name:[路远,温暖,向北,阳光,白雪,夏天,云朵],

Sex:[男,男,女,男,女,男,女],

Age:[20,19,22,23,18,20,22]}

df_left1=pd.DataFrame(data1)

data2={Score:[80,89,84,78,92.5,71]}

df_right1=pd.DataFrame(data2,index=[1002,1003,1004,1005,1006,1001])

df_left1.join(df_right1,how=left,on=Id)

输出结果:

显示全部
相似文档