删除符合条件的数据框行的一半 [英] Deleting half of dataframe rows which meet condition
问题描述
我正在基于条件提取数据帧的子集.假设
I'm looking to extract a subset of a dataframe based on a condition. Let's say
df = pd.Dataframe({'Col1': [values1], 'Col2' = [values2], 'Col3' = [values3]})
我想按Col2排序.在Col2中否定的条目(如果有)中,我想删除最大的一半.因此,如果values2 = [-5,10,13,-3,-1,-2],那么我想删除对应于值-5和-3的行.
I'd like to sort by Col2. Of the entries in Col2 that are negative (if any), I'd like to drop the largest half. So if values2 = [-5,10,13,-3,-1,-2], then I'd want to drop the rows corresponding to the values -5 and -3.
如果我想在排序后只丢弃整个数据帧的一半,我(认为)可以做到
If I wanted to simply drop half the entire dataframe after sorting, I (think) could do
df = df.iloc[(df.shape[0]/2):]
不确定如何引入仅减少负值一半的条件.我的大部分经验是在numpy中使用-仍然习惯于根据数据帧进行思考.预先感谢.
Not sure how to introduce the conditionality of dropping half of only the negative values. The vast majority of my experience is in numpy - still getting used to thinking in terms of dataframes. Thanks in advance.
推荐答案
一种简单的方法,首先,您希望对数据帧进行排序:
A straight-forward approach, first, you wanted your data-frame sorted:
In [16]: df = pd.DataFrame({'Col1': values1, 'Col2':values2, 'Col3': values3})
In [17]: df
Out[17]:
Col1 Col2 Col3
0 1 -5 a
1 2 10 b
2 3 13 c
3 4 -3 d
4 5 -1 e
5 6 -2 f
In [18]: df.sort_values('Col2', inplace=True)
In [19]: df
Out[19]:
Col1 Col2 Col3
0 1 -5 a
3 4 -3 d
5 6 -2 f
4 5 -1 e
1 2 10 b
2 3 13 c
然后,为负值创建一个布尔掩码,使用np.where
获取索引,将索引切成一半,然后删除这些索引:
Then, create a boolean mask for the negative values, use np.where
to get the indices, cut the indices and half, then drop those indices:
In [20]: mask = (df.Col2 < 0)
In [21]: idx, = np.where(mask)
In [22]: df.drop(df.index[idx[:len(idx)//2]])
Out[22]:
Col1 Col2 Col3
5 6 -2 f
4 5 -1 e
1 2 10 b
2 3 13 c
这篇关于删除符合条件的数据框行的一半的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!