怎么剔除掉六十岁(退休)以上的人(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()的用法相同
相关推荐
Uncommon.7 小时前
使用pandas处理csv并转为张量
pytorch·python·深度学习·pandas
STR_Liang2 天前
pandas.read_html 报错FileNotFoundError、OSError Traceback (most recent call last)
pandas
㳺三才人子3 天前
初探 Data Analysis - Matplotlib
python·plotly·pandas·matplotlib
quantdash_cc4 天前
告别自建 Requests/BS4 网页爬虫:基于 QuantDash 搭建零维保的高性能量化行情流水线
开发语言·爬虫·python·pandas·量化·quantdash
菜冻鱼4 天前
Python-sklearn-评估指标
开发语言·人工智能·python·机器学习·numpy·pandas·sklearn
菜冻鱼4 天前
Python-sklearn-模型选择
开发语言·人工智能·python·机器学习·numpy·pandas·sklearn
quantdash_cc4 天前
基于 QuantDash 5 分钟 K 线的网格交易策略参数网格搜索寻优实战
android·开发语言·pandas·量化·quantdash
FBI HackerHarry浩4 天前
Pandas 库中用于分类变量独热编码(One-Hot Encoding)的函数 pd.get_dummies() 函数
开发语言·人工智能·python·pandas
菜冻鱼8 天前
Python-pandas-索引与筛选
开发语言·笔记·python·numpy·pandas·学习方法
yanweijie03178 天前
Pandas 完整数据分析工作流实战(标准 5 步流程)
数据挖掘·数据分析·pandas