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 @ 2024-06-17 21:03  chuangzhou  阅读(1)  评论(0编辑  收藏  举报