Hi, When im working with some big dataframes and I need to create some columns based on functions. So i have some code like this

Def function(row): function

And then I run the function on the df as

df[‘new column’] = df.apply(function, axis=1)

But I do this with 10 or more columns/functions at time. I don’t think this is efficient because each time a column is created it had to parce the entire data frame. There’s a way to create all the columns at the same time while parsing the rows only once?

Thanks for any help.

  • driving_crooner@lemmy.eco.brOP
    link
    fedilink
    arrow-up
    3
    ·
    edit-2
    1 year ago

    Actually this seems like a better solution for you.

    Here’s another approach, I like this one more because it is a closer match to the problem you described.

    Thanks, tried the first approach but was slower that what I was doing. The second one didn’t worked because I use some of the new generated columns to create new ones, but doing the process twice, to use the new columns to create the additional columns worked well and reduced the process time from 22m to 13m. Maybe they’re ways to optimize even more the code, but 13 minutes is good enough for me.

    Edit: for some reason it broke the information in some way and the next steps of the process are giving me errors 😐

    Edit2: I’m an idiot, I made an error while updating the code to the new method.