如何通过列值的条件删除 DataFrame 中的行?
在本文中,我们将看到几个示例,说明如何根据应用于列的某些条件从数据框中删除行。
Pandas 为数据分析师提供了一种使用dataframe.drop()
方法删除和过滤数据框的方法。我们可以使用此方法删除不满足给定条件的行。
让我们创建一个 Pandas 数据框。
# import pandas library
import pandas as pd
# dictionary with list object in values
details = {
'Name' : ['Ankit', 'Aishwarya', 'Shaurya',
'Shivangi', 'Priya', 'Swapnil'],
'Age' : [23, 21, 22, 21, 24, 25],
'University' : ['BHU', 'JNU', 'DU', 'BHU',
'Geu', 'Geu'],
}
# creating a Dataframe object
df = pd.DataFrame(details, columns = ['Name', 'Age',
'University'],
index = ['a', 'b', 'c', 'd', 'e',
'f'])
df
输出:
示例 1:根据列上的条件删除行。
# import pandas library
import pandas as pd
# dictionary with list object in values
details = {
'Name' : ['Ankit', 'Aishwarya', 'Shaurya',
'Shivangi', 'Priya', 'Swapnil'],
'Age' : [23, 21, 22, 21, 24, 25],
'University' : ['BHU', 'JNU', 'DU', 'BHU',
'Geu', 'Geu'],
}
# creating a Dataframe object
df = pd.DataFrame(details, columns = ['Name', 'Age',
'University'],
index = ['a', 'b', 'c', 'd', 'e', 'f'])
# get names of indexes for which
# column Age has value 21
index_names = df[ df['Age'] == 21 ].index
# drop these row indexes
# from dataFrame
df.drop(index_names, inplace = True)
df
输出 :
示例 2:根据列上的多个条件删除行。
# import pandas library
import pandas as pd
# dictionary with list object in values
details = {
'Name' : ['Ankit', 'Aishwarya', 'Shaurya',
'Shivangi', 'Priya', 'Swapnil'],
'Age' : [23, 21, 22, 21, 24, 25],
'University' : ['BHU', 'JNU', 'DU', 'BHU',
'Geu', 'Geu'],
}
# creating a Dataframe object
df = pd.DataFrame(details, columns = ['Name', 'Age',
'University'],
index = ['a', 'b', 'c', 'd', 'e', 'f'])
# get names of indexes for which column Age has value >= 21
# and <= 23
index_names = df[ (df['Age'] >= 21) & (df['Age'] <= 23)].index
# drop these given row
# indexes from dataFrame
df.drop(index_names, inplace = True)
df
输出 :
示例 3:根据不同列上的多个条件删除行。
# import pandas library
import pandas as pd
# dictionary with list object in values
details = {
'Name' : ['Ankit', 'Aishwarya', 'Shaurya',
'Shivangi', 'Priya', 'Swapnil'],
'Age' : [23, 21, 22, 21, 24, 25],
'University' : ['BHU', 'JNU', 'DU', 'BHU',
'Geu', 'Geu'],
}
# creating a Dataframe object
df = pd.DataFrame(details, columns = ['Name', 'Age',
'University'],
index = ['a', 'b', 'c', 'd', 'e', 'f'])
# get names of indexes for which
# column Age has value >= 21
# and column University is BHU
index_names = df[ (df['Age'] >= 21) & (df['University'] == 'BHU')].index
# drop these given row
# indexes from dataFrame
df.drop(index_names, inplace = True)
df
输出 :