如何从 pandas 数据框中删除特定的行? [英] How to delete specific rows from pandas data frame?
问题描述
我正在处理如下所示的熊猫数据框.
I am dealing with a pandas data frame as shown below.
id x1 y1
0 2 some_val some_val
1 2 some_val some_val
2 2 some_val some_val
3 2 some_val some_val
4 2 some_val some_val
5 0 0 0
6 3 some_val some_val
7 3 some_val some_val
8 0 0 0
9 5 some_val some_val
10 5 some_val some_val
11 5 some_val some_val
12 0 0 0
13 6 some_val some_val
14 6 some_val some_val
15 6 some_val some_val
16 6 some_val some_val
我的原始数据帧是不包含全为"0"值的行的数据帧.根据项目要求,每当"id"更改时,我都必须插入全0的行.
My original data frame was the data frame without the rows with all '0' values. As per the project requirement I had to insert the rows with all 0's value whenever the "id" changes.
现在,我要删除具有3行且少于3行的任何"id"的所有行.从上面的数据帧中,我想删除id-"3"和"5"的所有相应行.我得到的数据框应如下所示:
Now I want to delete all the rows of any "id" which has 3 and less than 3 rows. From the above data frame, I would want to delete all the respective rows of id- "3" and "5" . My resultant data frame should look like below:
id x1 y1
0 2 some_val some_val
1 2 some_val some_val
2 2 some_val some_val
3 2 some_val some_val
4 2 some_val some_val
5 0 0 0
6 6 some_val some_val
7 6 some_val some_val
8 6 some_val some_val
9 6 some_val some_val
请给我建议一种获得此结果的方法.
Kindly suggest me a way to obtain this result.
推荐答案
最简单的答案是删除零行,因为如果其中有3个以上,它们可能会妨碍计算.然后做一个小组.然后过滤.然后像在其他问题/答案中一样添加回零
The simplest answer is to remove the zero rows because they may get in the way of the calculation if you have more than 3 of them. then do a group by. then filter. then add back zeros like you did in other question/answer
d1 = df.query('ProjID != 0').groupby('ProjID').filter(lambda df: len(df) > 3)
d1
ProjID Xcoord Ycoord
0 2 -7.863509 5.221327
1 2 some_val some_val
2 2 some_val some_val
3 2 some_val some_val
4 2 some_val some_val
13 6 some_val some_val
14 6 some_val some_val
15 6 some_val some_val
16 6 some_val some_val
然后重新添加
pidv = d1.ProjID.values
pid_chg = np.append(pidv[:-1] != pidv[1:], True)
i = d1.index.repeat(pid_chg + 1)
d2 = d1.loc[i, :].copy()
d2.loc[i.duplicated()] = 0
d2.reset_index(drop=True)
ProjID Xcoord Ycoord
0 2 -7.863509 5.221327
1 2 some_val some_val
2 2 some_val some_val
3 2 some_val some_val
4 2 some_val some_val
5 0 0 0
6 6 some_val some_val
7 6 some_val some_val
8 6 some_val some_val
9 6 some_val some_val
10 0 0 0
这篇关于如何从 pandas 数据框中删除特定的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!