怎么剔除掉六十岁(退休)以上的人(python自动化办公)

怎么剔除掉六十岁(退休)以上的人(python自动化办公)

需求分析:

python 复制代码
1.本代码的要求是从表1中根据姓名合并表2
2.删除掉为空的人数 ,后面再合并
3.表格内的19971111,所以首先需要得到年份
4.找出大于60岁的名单,输出名单,并删除掉60岁以上的人员

代码:

python 复制代码
    def get_le_six(self):
        # 读取两份表格文件
        table1 = pd.read_excel("coding_3.xlsx", sheet_name='Sheet1')  # 第一份表格
        table2 = pd.read_excel("coding_3.xlsx", sheet_name='Sheet2')  # 第二份表格

        # 合并两份表格,保留第一份表格的所有信息
        merged_table = pd.merge(table1, table2, on='姓名', how='left')

        # 找出匹配为空的行
        missing_matches = merged_table[merged_table['出生日期'].isna()]

        # 删除匹配为空的行
        merged_table = merged_table.dropna(subset=['出生日期'])

        # 根据出生日期计算每个人的年龄
        current_year = 2023
        merged_table['出生日期'] = merged_table['出生日期'].astype(str)
        merged_table['年龄'] = current_year - merged_table['出生日期'].str[:4].astype(int)
        # 找出大于六十岁的人名单
        older_than_sixty = merged_table[merged_table['年龄'] > 60]['姓名']

        # 打印大于六十岁的人名单
        print("大于六十岁的人名单:")
        print(older_than_sixty)

        # 从新表格中删除大于六十岁的人
        merged_table = merged_table[merged_table['年龄'] <= 60]

        # 合并匹配为空的人的姓名信息
        result = pd.concat([missing_matches, merged_table])

        # 保存结果为表格文件
        result.to_excel("missing_names.xlsx", index=False)
        merged_table.to_excel("new_table.xlsx", index=False)

重要知识点

json 复制代码
isna()
作用:判断是否为空值,返回True或False
(1)反义函数:notna()
(2)与isnull()的用法相同
相关推荐
jarreyer21 小时前
python,numpy,pandas和matplotlib版本对应关系
python·numpy·pandas
写代码的【黑咖啡】1 天前
Python中的Pandas:数据分析的利器
python·数据分析·pandas
laocooon5238578863 天前
对传入的 x , y 两个数组做折线图, x 对应 x 轴, y 对应 y 轴。并保存到 Task1/image1/T2.png
python·numpy·pandas·matplotlib
Maxwell_li14 天前
新冠检测例子学习查准率和召回率
学习·机器学习·数据分析·回归·numpy·pandas
渡我白衣4 天前
Python 与数据科学工具链入门:NumPy、Pandas、Matplotlib 快速上手
人工智能·python·机器学习·自然语言处理·numpy·pandas·matplotlib
IT北辰5 天前
用 Python 自动解析药品规格并计算包装总容量 —— pandas + 正则实战
开发语言·python·pandas
lbb 小魔仙5 天前
Python 读取 Excel 文件:openpyxl 与 pandas 实战对比
python·excel·pandas
Amber_375 天前
数据分析之(MySQL+普通程序) VS (Python的NumPy/Pandas)
python·mysql·数据分析·numpy·pandas
Lucky高6 天前
Pandas库实践3_索引
开发语言·python·pandas
墨上烟雨6 天前
Pandas读写CSV、Excel、JSON文件
pandas