请对数据框使用以下代码。
删除“'Sachin','Virat']在'2014'年的总行数超过100的所有行
smd = [['Sachin', 1989, 130],
['Virat', 2014, 99],
['Sachin', 2014, 99],
['Virat', 2014, 183],
['Virat', 2014, 183],
['Rohit', 2014, 79],
['Virat', 2015, 129],
['Raina', 2014, 183],
['Rohit', 2014, 264],
['Sachin', 2014, 130],]
df_smd = pd.DataFrame(smd, columns = ['Player', 'Year','Score'])
df_smd
答案 0 :(得分:1)
您只需要写下您的条件:
df.column_name == your_value
isin
进行操作。~
运算符。一旦具备条件,则可以使用&
运算符(而不是and
)应用所有条件。
代码在这里:
smd = [['Sachin', 1989, 130],
['Virat', 2014, 99],
['Sachin', 2014, 99],
['Virat', 2014, 183],
['Virat', 2014, 183],
['Rohit', 2014, 79],
['Virat', 2015, 129],
['Raina', 2014, 183],
['Rohit', 2014, 264],
['Sachin', 2014, 130]]
df_smd = pd.DataFrame(smd, columns=['Player', 'Year', 'Score'])
# Select row where player is 'Sachin' or 'Virat'
condition_name = df_smd.Player.isin(['Sachin', 'Virat'])
condition_score = df_smd.Score > 100
condition_year = df_smd.Year == 2014
# Apply all the conditions
output = df_smd[~(condition_name & condition_score & condition_year)]
print(output)
# Player Year Score
# 0 Sachin 1989 130
# 1 Virat 2014 99
# 2 Sachin 2014 99
# 5 Rohit 2014 79
# 6 Virat 2015 129
# 7 Raina 2014 183
# 8 Rohit 2014 264
您也可以使用drop
来做到这一点(即使我发现它不太自然。):
condition = (condition_name & condition_score & condition_year)
output = df_smd.drop(index=condition[condition].index)
print(output)
# Player Year Score
# 0 Sachin 1989 130
# 1 Virat 2014 99
# 2 Sachin 2014 99
# 5 Rohit 2014 79
# 6 Virat 2015 129
# 7 Raina 2014 183
# 8 Rohit 2014 264