我一直在尝试选择数据集中满足2个条件的行,然后从总数据集中随机删除其中25%的行。我一直在尝试将此处类似问题的代码拼凑在一起,但是我没有足够的python知识,也无法弄清楚我要去哪里。
我尝试了2种方法:
#Store rows meeting conditions in a variable
test = dataset[(dataset['betamax'].isnull()) & (dataset['label'] == "probable")]
#Only select 75% of them in a new variable
test2 = test.sample(frac=.75)
#Remove any matches from test2 in my total dataset
test3 = dataset[~dataset.isin(test2)].dropna()
test2
是146行乘84列,dataset
是750行乘84列。当我创建的test3
时候是0行乘84列-为什么会发生这种情况?
我还尝试通过以下方式删除对行的选择:
cond = dataset['Gene'].isin(test2['Gene']) #Gene is my only unique column per row
test4 = dataset.drop(dataset[cond].index, inplace = True)
TypeError: 'NoneType' object is not subscriptable
不幸的是,我无法提供示例数据,但是如果我有2个变量-一个变量是根据条件对子集进行随机分组,另一个变量是我的总数据,如何从总数据集中删除该子集?
在您的解决方案中inplace = True
,请删除,因为它返回了None
,所以不能分配给新变量test4
:
test4 = dataset.drop(dataset[cond].index)
更好的是通过反转掩码来~
获得不存在的值test2['Gene']
:
cond = dataset['Gene'].isin(test2['Gene'])
test4 = dataset[~cond]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句