When you want to loop around Python operators,

Asked 2 years ago, Updated 2 years ago, 16 views

Navigating relationships between the Pandas columns.
By drawing two columns, you want to explore which operation the target column comes from between which two columns.

# This code is pseudo code based on python. It will not run. 
For i, j in combinations (df.columns, r=2): #itertools' combinations 
    For operator in [+, -, *, /]: # Core line of problem
        val_tmp = operator(df[i], df[j])
        if RMSE( df.target - val_tmp ) <0.01:
            print(i, operator, j)

The purpose is simple, but I don't know how to handle the looping part of those operators.

I think there's a smart way to deal with Ifelse, instead of tinkering with it and making it dirty.

python

2022-09-20 10:22

1 Answers

Try using Lambda.

>>> for a, b in zip([1, 2, 3], [4, 5, 6]):
    for op in [ lambda a, b: a+b, lambda a, b: a-b]:
        print(a, b, op(a, b))


1 4 5
1 4 -3
2 5 7
2 5 -3
3 6 9
3 6 -3


2022-09-20 10:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.