在Pandas数据框中的多个条件下删除行 [英] Drop rows on multiple conditions in pandas dataframe
本文介绍了在Pandas数据框中的多个条件下删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的df有3列
df = pd.DataFrame({"col_1": (0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0),
"col_2": (0.0, 0.24, 1.0, 0.0, 0.22, 3.11, 0.0),
"col_3": ("Mon", "Tue", "Thu", "Fri", "Mon", "Tue", "Thu")})
我想删除df.col_1为1.0且df.col_2为0.0的行.所以,我会得到:
I want to drop rows where df.col_1 is 1.0 and df.col_2 is 0.0. So, I would get:
df = pd.DataFrame({"col_1": (0.0, 0.0, 1.0, 0.0, 1.0),
"col_2": (0.0, 0.24, 1.0, 0.22, 3.11),
"col_3": ("Mon", "Tue", "Thu", "Mon", "Tue")})
我尝试过:
df_new = df.drop[df[(df['col_1'] == 1.0) & (df['col_2'] == 0.0)].index]
它给了我错误:
'method' object is not subscriptable
有什么办法解决上述问题吗?
Any idea how to solve the above problem?
推荐答案
drop is a method, you are calling it using []
, that is why it gives you:
'method' object is not subscriptable
更改为()
(常规方法调用),它应该可以工作:
change to ()
(a normal method call) an it should work:
import pandas as pd
df = pd.DataFrame({"col_1": (0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0),
"col_2": (0.0, 0.24, 1.0, 0.0, 0.22, 3.11, 0.0),
"col_3": ("Mon", "Tue", "Thu", "Fri", "Mon", "Tue", "Thu")})
df_new = df.drop(df[(df['col_1'] == 1.0) & (df['col_2'] == 0.0)].index)
print(df_new)
输出
col_1 col_2 col_3
0 0.0 0.00 Mon
1 0.0 0.24 Tue
2 1.0 1.00 Thu
4 0.0 0.22 Mon
5 1.0 3.11 Tue
这篇关于在Pandas数据框中的多个条件下删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文