0

Im trying to learn pandas and do things in a pandas way. I have a DataFrame, where i want to apply some sort of function to some columns. I did something that it works but i want to know that is a pandas way or if did it "wrong". The thing is that my dataframe now is about of 400 000 rows and 21 columns, but then will be really bigger than now, so i want to do all the "best" way. I leave an example. To the DataFrame, i want to operate with a column and to each row of that column apply a function that use values of other columns but again of the same row. Here is more or less what i did, this works but is pandas thinking? There is an efficient way to do something like this?

def resultWohKomi(orders,outcomes,komis):  # outcomes and komis are floats
    try:   
        result = orders[1] # order is a [1,0] or [0,1]
        when_black_win_then_positive_else_negative = 2*result-1
        points =  when_black_win_then_positive_else_negative*(outcomes + komis)
        if points > 0:
            result = [1,0]
        else:
            result = [0,1]
        return result
    except:       
        return orders

Type_new = df.apply(lambda x: resultWohKomi(x.order,x.outcome,x.komi), axis=1)
df.insert(2, "results",Type_new) 

df.order are [1,0] or [0,1] (list of int)

df.outcome are floats like 19.0

df.komi are floats like 6.5

mazzeta
  • 59
  • 4
  • What is your data look like (just few rows)? And what is the expected output if it cannot be generated by the given function,? – Quang Hoang Apr 24 '20 at 20:56
  • https://stackoverflow.com/questions/52673285/performance-of-pandas-apply-vs-np-vectorize-to-create-new-column-from-existing-c -- may this would help you in optimizing your code. – Rajat Mishra Apr 24 '20 at 21:17

0 Answers0