pandas操作excel-14-dataFrame-merge/join

眉间皱痕 提交于 2020-02-29 18:42:57

输出各列之间的相关性:

import pandas as pd

homes = pd.read_excel('D:/output.xlsx', index_col='idx')
# 输出各列之间的相关性
print(homes.corr())

dataFrame-merge/join:

import pandas as pd

# 索引列:学员Id,数据列:学员姓名
students = pd.read_excel('D:/output.xlsx', index_col='idx', sheet_name='Student')
# 索引列:学员Id, 数据列: 学员成绩
scores = pd.read_excel('D:/output.xlsx', index_col='idx', sheet_name='Score')

# 合并数据集
# 方法1,merge
# 不写on的时候, merge 默认从两边的两张数据表中找相同名称的列进行join
# 使用左连接,保留所有学员,填充空值为0
# on 指定两个数据集的列
studentsScores = students.merge(scores, on='idx', how='left').fillna(0)
# 左右两边的列名不一样的时候,使用left_on和right_on
studentsScores = students.merge(scores, left_on='idx', right_on='idx', how='left').fillna(0)


# 转换浮点类型为正数
studentsScores.Score = studentsScores.Score.astype(int)


# 方法2: join 默认使用index 进行连接on操作
# join 函数去掉了left_on 和 right_on 
studentsScores = students.join(scores, how='left').fillna(0)

print(studentsScores)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!