Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Remove entries by two-column ID everywhere, that meet a condition somewhere

Parent

Remove entries by two-column ID everywhere, that meet a condition somewhere

+2
−0

MWE

import random
import pandas as pd

from itertools import product


random.seed(12345)

dies = [1, 2]
cells = list(range(10))
currents = [100, 200, 300]

dcc = list(product(dies, cells, currents))
resistances = random.choices(range(250000 + 1), k=len(dcc))

df = pd.DataFrame(dcc, columns=["Die", "Cell", "Current"])
df["Resistance"] = resistances

b100 = df[(df["Current"] == 100) & (df["Resistance"] < 100000)]
df:
    Die  Cell  Current  Resistance
0     1     0      100      104155
1     1     0      200        2542
2     1     0      300      206302
3     1     1      100       74660
4     1     1      200       92103
5     1     1      300       48415
6     1     2      100      141502
7     1     2      200       40422
8     1     2      300       31066
9     1     3      100      108234
10    1     3      200      140520
11    1     3      300       43586
12    1     4      100      138305
13    1     4      200       88725
14    1     4      300      239517
15    1     5      100       22823
16    1     5      200      244660
17    1     5      300      103030
18    1     6      100      125984
19    1     6      200       37036
20    1     6      300      179742
21    1     7      100       47493
22    1     7      200       85390
23    1     7      300        5880
24    1     8      100       84879
25    1     8      200      241871
26    1     8      300      244700
27    1     9      100      186133
28    1     9      200         863
29    1     9      300      235060
30    2     0      100      217692
31    2     0      200      192709
32    2     0      300       44718
33    2     1      100       24875
34    2     1      200      103633
35    2     1      300      221385
36    2     2      100      144522
37    2     2      200      184146
38    2     2      300       58155
39    2     3      100      130899
40    2     3      200      177347
41    2     3      300      206209
42    2     4      100      201781
43    2     4      200       58077
44    2     4      300      218298
45    2     5      100       54095
46    2     5      200      200475
47    2     5      300      138771
48    2     6      100       46457
49    2     6      200      147152
50    2     6      300      129560
51    2     7      100      239666
52    2     7      200       10384
53    2     7      300       41034
54    2     8      100      245824
55    2     8      200      208052
56    2     8      300       37568
57    2     9      100       57278
58    2     9      200      134785
59    2     9      300       39245
b100:
    Die  Cell  Current  Resistance
3     1     1      100       74660
15    1     5      100       22823
21    1     7      100       47493
24    1     8      100       84879
33    2     1      100       24875
45    2     5      100       54095
48    2     6      100       46457
57    2     9      100       57278

Problem

If a cell is below 100k resistance, at 100 current, I want to remove that cell everywhere, within that die. b100 above shows that I want to remove all entries for cells 1, 5, 7, and 8 in die 1, and all entries for cells 1, 5, 6, and 9 in die 2.

Cells 6 and 9 should not be removed from die 1, and cells 7 and 8 should not be removed from die 2.

How would I do this programatically?

Tried

Tried:

df = df.mask((df["Die"].isin(b100["Die"])) & (df["Cell"].isin(b100["Cell"])))

But this removes every cell marked, in either die:

  Die  Cell  Current  Resistance
0   1.0   0.0    100.0    104155.0
1   1.0   0.0    200.0      2542.0
2   1.0   0.0    300.0    206302.0
3   NaN   NaN      NaN         NaN
4   NaN   NaN      NaN         NaN
5   NaN   NaN      NaN         NaN
6   1.0   2.0    100.0    141502.0
7   1.0   2.0    200.0     40422.0
8   1.0   2.0    300.0     31066.0
9   1.0   3.0    100.0    108234.0
10  1.0   3.0    200.0    140520.0
11  1.0   3.0    300.0     43586.0
12  1.0   4.0    100.0    138305.0
13  1.0   4.0    200.0     88725.0
14  1.0   4.0    300.0    239517.0
15  NaN   NaN      NaN         NaN
16  NaN   NaN      NaN         NaN
17  NaN   NaN      NaN         NaN
18  NaN   NaN      NaN         NaN
19  NaN   NaN      NaN         NaN
20  NaN   NaN      NaN         NaN
21  NaN   NaN      NaN         NaN
22  NaN   NaN      NaN         NaN
23  NaN   NaN      NaN         NaN
24  NaN   NaN      NaN         NaN
25  NaN   NaN      NaN         NaN
26  NaN   NaN      NaN         NaN
27  NaN   NaN      NaN         NaN
28  NaN   NaN      NaN         NaN
29  NaN   NaN      NaN         NaN
30  2.0   0.0    100.0    217692.0
31  2.0   0.0    200.0    192709.0
32  2.0   0.0    300.0     44718.0
33  NaN   NaN      NaN         NaN
34  NaN   NaN      NaN         NaN
35  NaN   NaN      NaN         NaN
36  2.0   2.0    100.0    144522.0
37  2.0   2.0    200.0    184146.0
38  2.0   2.0    300.0     58155.0
39  2.0   3.0    100.0    130899.0
40  2.0   3.0    200.0    177347.0
41  2.0   3.0    300.0    206209.0
42  2.0   4.0    100.0    201781.0
43  2.0   4.0    200.0     58077.0
44  2.0   4.0    300.0    218298.0
45  NaN   NaN      NaN         NaN
46  NaN   NaN      NaN         NaN
47  NaN   NaN      NaN         NaN
48  NaN   NaN      NaN         NaN
49  NaN   NaN      NaN         NaN
50  NaN   NaN      NaN         NaN
51  NaN   NaN      NaN         NaN
52  NaN   NaN      NaN         NaN
53  NaN   NaN      NaN         NaN
54  NaN   NaN      NaN         NaN
55  NaN   NaN      NaN         NaN
56  NaN   NaN      NaN         NaN
57  NaN   NaN      NaN         NaN
58  NaN   NaN      NaN         NaN
59  NaN   NaN      NaN         NaN

Tried:

rm_dies = b100["Die"].to_list()
rm_cells = b100["Cell"].to_list()

for die, cell in zip(rm_dies, rm_cells):
    df = df.mask((df["Die"] == die) & (df["Cell"] == cell))

This works, but is very slow on large dataframes and is not elegant.

    Die  Cell  Current  Resistance
0   1.0   0.0    100.0    104155.0
1   1.0   0.0    200.0      2542.0
2   1.0   0.0    300.0    206302.0
3   NaN   NaN      NaN         NaN
4   NaN   NaN      NaN         NaN
5   NaN   NaN      NaN         NaN
6   1.0   2.0    100.0    141502.0
7   1.0   2.0    200.0     40422.0
8   1.0   2.0    300.0     31066.0
9   1.0   3.0    100.0    108234.0
10  1.0   3.0    200.0    140520.0
11  1.0   3.0    300.0     43586.0
12  1.0   4.0    100.0    138305.0
13  1.0   4.0    200.0     88725.0
14  1.0   4.0    300.0    239517.0
15  NaN   NaN      NaN         NaN
16  NaN   NaN      NaN         NaN
17  NaN   NaN      NaN         NaN
18  1.0   6.0    100.0    125984.0
19  1.0   6.0    200.0     37036.0
20  1.0   6.0    300.0    179742.0
21  NaN   NaN      NaN         NaN
22  NaN   NaN      NaN         NaN
23  NaN   NaN      NaN         NaN
24  NaN   NaN      NaN         NaN
25  NaN   NaN      NaN         NaN
26  NaN   NaN      NaN         NaN
27  1.0   9.0    100.0    186133.0
28  1.0   9.0    200.0       863.0
29  1.0   9.0    300.0    235060.0
30  2.0   0.0    100.0    217692.0
31  2.0   0.0    200.0    192709.0
32  2.0   0.0    300.0     44718.0
33  NaN   NaN      NaN         NaN
34  NaN   NaN      NaN         NaN
35  NaN   NaN      NaN         NaN
36  2.0   2.0    100.0    144522.0
37  2.0   2.0    200.0    184146.0
38  2.0   2.0    300.0     58155.0
39  2.0   3.0    100.0    130899.0
40  2.0   3.0    200.0    177347.0
41  2.0   3.0    300.0    206209.0
42  2.0   4.0    100.0    201781.0
43  2.0   4.0    200.0     58077.0
44  2.0   4.0    300.0    218298.0
45  NaN   NaN      NaN         NaN
46  NaN   NaN      NaN         NaN
47  NaN   NaN      NaN         NaN
48  NaN   NaN      NaN         NaN
49  NaN   NaN      NaN         NaN
50  NaN   NaN      NaN         NaN
51  2.0   7.0    100.0    239666.0
52  2.0   7.0    200.0     10384.0
53  2.0   7.0    300.0     41034.0
54  2.0   8.0    100.0    245824.0
55  2.0   8.0    200.0    208052.0
56  2.0   8.0    300.0     37568.0
57  NaN   NaN      NaN         NaN
58  NaN   NaN      NaN         NaN
59  NaN   NaN      NaN         NaN

Notes

Ultimately I want to remove these cells from the dataframe. dropna() excluded from above calls for illustrative purposes.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+1
−1

You were on the right path breaking it down into two steps. First identify the cells you want to remove:

# Identify the 'Cell' values that meet the criteria
cells_to_remove = df[(df['Resistance'] < 100000) & (df['Current'] == 100)]['Cell'].unique()

And then remove them:

# Remove all rows with 'Cell' values that were identified
filtered_df = df[~df['Cell'].isin(cells_to_remove)]
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

The problem with this answer is that it will remove cells in every die, when it should only remove ce... (1 comment)
The problem with this answer is that it will remove cells in every die, when it should only remove ce...
mcp‭ wrote over 1 year ago

The problem with this answer is that it will remove cells in every die, when it should only remove cells identified within that die.

With your answer, if I find that die 1, cell 9 is bad, I will also be removing die 2, cell 9, which may not be bad.