如何在 Pandas 中删除包含特定值的行?
在本文中,我们将讨论如何在 Pandas 中删除包含特定值的行。删除行意味着从数据框中删除值,我们可以使用条件或关系运算符删除特定值。
方法一:使用运算符删除特定值
我们可以使用 column_name函数和运算符来删除特定值。
Syntax: dataframe[dataframe.column_name operator value]
where
- dataframe is the input dataframe
- column_name is the value of that column to be dropped
- operator is the relational operator
- value is the specific value to be dropped from the particular column
使用不同的运算符删除列
Python3
# import pandas module
import pandas as pd
# create dataframe with 4 columns
data = pd.DataFrame({
"name": ['sravan', 'jyothika', 'harsha', 'ramya',
'sravan', 'jyothika', 'harsha', 'ramya',
'sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'java', 'java', 'python',
'python', 'python', 'html/php', 'html/php',
'html/php', 'php/js', 'php/js', 'php/js'],
"marks": [98, 79, 89, 97, 82, 98, 90,
87, 78, 89, 93, 94]
})
# display
print(data)
print("---------------")
# drop rows where value is 98
# by using not equal operator
print(data[data.marks != 98])
print("---------------")
Python3
# import pandas module
import pandas as pd
# create dataframe with 4 columns
data = pd.DataFrame({
"name": ['sravan', 'jyothika', 'harsha', 'ramya',
'sravan', 'jyothika', 'harsha', 'ramya',
'sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'java', 'java', 'python',
'python', 'python', 'html/php',
'html/php', 'html/php', 'php/js',
'php/js', 'php/js'],
"marks": [98, 79, 89, 97, 82, 98, 90, 87,
78, 89, 93, 94]
})
# consider the list
list1 = [98, 82, 79]
# drop rows from above list
print(data[data.marks.isin(list1) == False])
print("---------------")
list2 = ['sravan', 'jyothika']
# drop rows from above list
print(data[data.name.isin(list2) == False])
Python3
# import pandas module
import pandas as pd
# create dataframe with 4 columns
data = pd.DataFrame({
"name": ['sravan', 'jyothika', 'harsha', 'ramya',
'sravan', 'jyothika', 'harsha', 'ramya',
'sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'java', 'java', 'python',
'python', 'python', 'html/php',
'html/php', 'html/php', 'php/js',
'php/js', 'php/js'],
"marks": [98, 79, 89, 97, 82, 98, 90,
87, 78, 89, 93, 94]
})
# drop specific values
# where marks is 98 and name is sravan
print(data[(data.marks != 98) & (data.name != 'sravan')])
print("------------------")
# drop specific values
# where marks is 98 or name is sravan
print(data[(data.marks != 98) | (data.name != 'sravan')])
输出:
方法 2:删除列表中包含值的行
通过使用这种方法,我们可以删除列表中存在的多个值,我们使用的是 isin()运算符。此运算符用于检查给定值是否存在于列表中
Syntax: dataframe[dataframe.column_name.isin(list_of_values) == False]
where
- dataframe is the input dataframe
- column_name is to remove values in this column
- list_of_values is the specific values to be removed
Python3
# import pandas module
import pandas as pd
# create dataframe with 4 columns
data = pd.DataFrame({
"name": ['sravan', 'jyothika', 'harsha', 'ramya',
'sravan', 'jyothika', 'harsha', 'ramya',
'sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'java', 'java', 'python',
'python', 'python', 'html/php',
'html/php', 'html/php', 'php/js',
'php/js', 'php/js'],
"marks": [98, 79, 89, 97, 82, 98, 90, 87,
78, 89, 93, 94]
})
# consider the list
list1 = [98, 82, 79]
# drop rows from above list
print(data[data.marks.isin(list1) == False])
print("---------------")
list2 = ['sravan', 'jyothika']
# drop rows from above list
print(data[data.name.isin(list2) == False])
输出:
方法 3:删除多列中包含特定值的行
我们可以使用关系运算符从多个列中删除特定值。
Syntax: dataframe[(dataframe.column_name operator value ) relational_operator (dataframe.column_name operator value )]
where
- dataframe is the input dataframe
- column_nam eis the column
- operator is the relational operator
Python3
# import pandas module
import pandas as pd
# create dataframe with 4 columns
data = pd.DataFrame({
"name": ['sravan', 'jyothika', 'harsha', 'ramya',
'sravan', 'jyothika', 'harsha', 'ramya',
'sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'java', 'java', 'python',
'python', 'python', 'html/php',
'html/php', 'html/php', 'php/js',
'php/js', 'php/js'],
"marks": [98, 79, 89, 97, 82, 98, 90,
87, 78, 89, 93, 94]
})
# drop specific values
# where marks is 98 and name is sravan
print(data[(data.marks != 98) & (data.name != 'sravan')])
print("------------------")
# drop specific values
# where marks is 98 or name is sravan
print(data[(data.marks != 98) | (data.name != 'sravan')])
输出: