Python - pandas 利用 某一列的值过滤数据

# FA存在3D不存在建模的代码(1).txt
EDLG-S1-M3-L12

有一个excel:

需求:
利用txt 中的代码去匹配execl 中的 调整后的规格型号,将匹配的数据保留,生成新的excel

import pandas


with open('FA存在3D不存在建模的代码(1).txt', 'r') as f:
    txt_codes = {item.replace('\n', '') for item in f.readlines()}

df = pandas.read_excel('转化调整明细.xlsx')

# 新增一列 标记行
df['KeepRow'] = df['调整后规格型号'].apply(lambda code: code in txt_codes)

print(df['KeepRow'])
'''
1    True
'''


# 过滤为True的行
df = df[df['KeepRow']]
print(df)
'''
             原规格型号   调整数量         调整后规格型号          备注  KeepRow
1  EDLW-S1-M1.6-L4  31961  EDLG-S1-M3-L12  fasfdsdfas     True
'''

with pandas.ExcelWriter('test.xlsx') as w:
df.to_excel(w, sheet_name='final', index=False, columns=df.columns[:-1])
posted @   chuangzhou  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示