程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[pandas tip] filter and delete the row where the target value is located

編輯:Python
Enjoy the beautiful picture 2022/06/21

1. Filter out the row of the target value  

Single column filter

# df[ Name ].isin([ The target ]) For the row with target value in the current column, it will return True, There is no return False
df[df[ Name ].isin([ The target ])]

Practice cases  

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'])
# select df_bom_data The table contains only df_material_shortage_data In the table Material The line record of
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( After processing )

Multi column filtering

# At the same time & Connect , Or | Connect
df[df[ Name ].isin([ The target ]) & df[ Name ].isin([ The target ])]
df[df[ Name ].isin([ The target ]) | df[ Name ].isin([ The target ])]

Practice cases  

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'])
# Filter out the rows with target values in the specified columns
res1 = df[df['Level'].isin(['A','C']) & df['Passing'].isin([0])]
# Filter out at least one row with a target value
res2 = df[df['Level'].isin(['A','C']) | df['Passing'].isin([0])]

df

res1

res2 

2. Delete the row where the target value is located

Practice cases

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'])
# select df_bom_data in 'Price' and 'Quantity' The values of both columns of fields are empty (nans) The line of
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 Table delete all_isnull_df_bom_data In the table 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( After processing )


Extended supplementary cases : Delete the row where the column is the specified value

import pandas as pd
df = pd.DataFrame([[0,1,2,3],
[4,5,6,7],
[8,9,10,11]
],columns=['A','B','C','D'])
# By taking a new value , Re assign values after data filtering , To delete row data whose column is the specified value
# Delete A The value in the column is 0 That line of records
df = df[df['A'] != 0]

df

df( After processing ) 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved