从数据集中删除行

时间:2019-06-11 06:51:41

标签: python-3.x pandas numpy dataset

我有一个数据集,如下所示:

   Attribute:Value  Support
0            VDM:1        9
1            VDM:2        2
2            VDM:3        0
3            VDM:4        0
4            VDM:5        1
5            MDM:1        2
6            MDM:2        6
7            MDM:3        0
8            MDM:4        3
9            MDM:5        1
10            OM:1        2
11            OM:2        6
12            OM:3        0
13            OM:4        3
14            OM:5        1

在这里我要删除那些支持小于或等于4且attribute:value对的值为1或2或3的行。删除行后,数据集将如下所示:

   Attribute:Value  Support
0            VDM:1        9
1            VDM:4        0
2            VDM:5        1
3            MDM:2        6
4            MDM:4        3
5            MDM:5        1
6             OM:2        6
7             OM:4        3
8             OM:5        1

值部分将仅包含1,2,3,4,5。

3 个答案:

答案 0 :(得分:2)

使用boolean indexing删除行-但条件必须反转-因此将Son的{​​{1}}用作OR,将Son用作OR,将第一个掩码使用{{1} }用于反转掩码并用于第二条件Series.gt getMe()用于反转Son

对于Series.str.splitSeries.str.extract使用&之后的值:

AND

因为:

  

值部分将仅包含1,2,3,4,5。

可以使用:

|

~

答案 1 :(得分:1)

我认为您正在寻找这个

s=(df['Attribute:Value'].str.split(':').str[-1]).astype(int)
df=df[(df['Support']>4)|(s>3)]

O / P:

   Attribute:Value  Support
0            VDM:1        9
3            VDM:4        0
4            VDM:5        1
6            MDM:2        6
8            MDM:4        3
9            MDM:5        1
11            OM:2        6
13            OM:4        3
14            OM:5        1

说明:

  1. 分割属性和值
  2. 保持行大于3或支持大于4。

答案 2 :(得分:1)

您可以使用:

df[~(df['Attribute:Value'].str.split(':').str[1].isin(['1','2','3'])&df.Support.le(4))]

   Attribute:Value  Support
0            VDM:1        9
3            VDM:4        0
4            VDM:5        1
6            MDM:2        6
8            MDM:4        3
9            MDM:5        1
11            OM:2        6
13            OM:4        3
14            OM:5        1