1.筛选出目标值所在行
单列筛选
# df[列名].isin([目标值])对当前列中存在目标值的行会返回True,不存在的返回False
df[df[列名].isin([目标值])]
练习案例
import pandas as pd
df_bom_data = pd.DataFrame([['A123',1200,5],
['B456',550,2],
['C437',500,10],
['D112',621,7],
['E211',755,11],
['F985',833,8]
],columns=['Material','Price','Quantity'])
df_material_shortage_data = pd.DataFrame([['A123','2022/6/21',100],
['B456','2022/6/22',120],
['C437','2022/6/23',250]
],columns=['Material','Schedule','LT'])
# 筛选出df_bom_data表中只包含df_material_shortage_data表中Material的行记录
df_bom_data = df_bom_data[df_bom_data['Material'].isin(df_material_shortage_data['Material'])]
df_bom_data
df_material_shortage_data
df_bom_data(处理后)
多列筛选
# 同时满足用&连接,或的话用 | 连接
df[df[列名].isin([目标值]) & df[列名].isin([目标值])]
df[df[列名].isin([目标值]) | df[列名].isin([目标值])]
练习案例
import pandas as pd
df = pd.DataFrame([['L123','A',0],
['L456','A',1],
['L437','C',0],
['L112','B',1],
['L211','A',0],
['L985','B',1]
],columns=['Material','Level','Passing'])
# 筛选出指定列都有目标值的行
res1 = df[df['Level'].isin(['A','C']) & df['Passing'].isin([0])]
# 筛选出至少有一列有目标值的行
res2 = df[df['Level'].isin(['A','C']) | df['Passing'].isin([0])]
df
res1
res2
2.删除目标值所在的行
练习案例
import pandas as pd
import numpy as np
df_bom_data = pd.DataFrame([['A123',1200,5],
['B456',np.nan,np.nan],
['C437',500,10]
],columns=['Material','Price','Quantity'])
df_material_shortage_data = pd.DataFrame([['A123','2022/6/21',100],
['B456','2022/6/22',120],
['C437','2022/6/23',250]
],columns=['Material','Schedule','LT'])
# 筛选出df_bom_data中'Price'和'Quantity'两列字段的值都为空(nans)的行
df_isnull_bom_data = df_bom_data[pd.isnull(df_bom_data[df_bom_data.columns.tolist()[1:]]).all(axis=1)]
# df_material_shortage_data表删除all_isnull_df_bom_data表中的Material
df_material_shortage_data = df_material_shortage_data[~df_material_shortage_data['Material'].isin(df_isnull_bom_data['Material'])]
df_bom_data
df_material_shortage_data
df_isnull_bom_data
df_material_shortage_data(处理后)
扩展补充案例:删除列为指定值所在的行
import pandas as pd
df = pd.DataFrame([[0,1,2,3],
[4,5,6,7],
[8,9,10,11]
],columns=['A','B','C','D'])
# 通过重新取值,数据筛选后重新赋值,达到删除列为指定值的行数据
# 删除A列中值为0的那一行记录
df = df[df['A'] != 0]
df
df(处理后)