我有一个数据集如下所示:
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且属性值: 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。
发布于 2019-06-11 06:57:59
使用boolean indexing
删除行,但条件是必须反转的,因此AND
的&
用于OR,对于第一个掩码使用~
作为倒置掩码,对于第二个条件使用Series.gt
>
表示倒置<=
。
也适用于:
使用Series.str.split
或Series.str.extract
后的值。
mask = ~df['Attribute:Value'].str.split(':').str[1].isin(['1','2','3']) | df['Support'].gt(4)
因为:
价值部分只包含1,2,3,4,5。
是否有可能使用:
mask = (df['Attribute:Value'].str.extract(':(\d+)', expand=False).astype(int).gt(3) |
df['Support'].gt(4))
df1 = df[mask]
print (df1)
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
发布于 2019-06-11 06:59:19
我想你是在找这个,
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
说明:
发布于 2019-06-11 06:59:50
您可以使用:
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
https://stackoverflow.com/questions/56538069
复制相似问题