I have a dataframe in the following format:
| Index | Object1-Length | Object1-Height | Object2-Length | Object2-Height |
|---|---|---|---|---|
| 0 | 19 | 49 | 21 | 52 |
| 1 | 20 | 50 | 21 | 51 |
| 2 | 20 | 51 | 20 | 52 |
| 3 | 19 | 50 | 19 | 52 |
| 4 | 20 | 50 | 20 | 52 |
It continues with Object3, Object4 and so on...
I want to check two columns at the same time in the following way:
if ObjectX-Length >= 20 and ObjectX-Height >= 50
then set both cells of ObjectX to 1, else set them to 0
So this is the table I want:
| Index | Object1-Length | Object1-Height | Object2-Length | Object2-Height |
|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 1 |
| 2 | 1 | 1 | 1 | 1 |
| 3 | 0 | 0 | 0 | 0 |
| 4 | 1 | 1 | 1 | 1 |
Is there any way to do this?
EDIT: Combining both columns of each object into one column and setting that cell to 0 or 1 would also work!
Let's try:
# length like columns
l = df.filter(like='-Length').columns
# corresponding height columns
h = l.str.rstrip('Length') + 'Height'
# create boolean mask
m = (df[l].ge(20).values & df[h].ge(50).values).astype(int)
# assign the values
df[h], df[l] = m, m
Details:
First filter the Length like columns, then create the corresponding Height columns:
print(l)
['Object1-Length', 'Object2-Length']
print(h)
['Object1-Height', 'Object2-Height']
Create boolean mask representing the condition where the ObjectX-Length >= 20 and ObjectX-Height >= 50:
print(m)
array([[0, 1],
[1, 1],
[1, 1],
[0, 0],
[1, 1]])
assign the mask to corresponding columns:
print(df)
Object1-Length Object1-Height Object2-Length Object2-Height
Index
0 0 0 1 1
1 1 1 1 1
2 1 1 1 1
3 0 0 0 0
4 1 1 1 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With