[Python] How to vectorize my code in pandas? it is long and inefficient

Discussão em 'Python' iniciado por Stack, Janeiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I have this code that I wrote and it's taking too long to run. I was advised to vectorize this operation but so far I have found only multiplication examples. Here is my code:

    my_dict = {}
    for i in list(df.index):
    my_dict = myClass(df.loc[i, 'name'])
    my_dict.class_method({'col1': df.loc[i, 'col1']})
    my_dict.class_method({'col2': df.loc[i, 'col2']})
    ...


    and so on until 'col17'. Someone reviewed my code and said to 'use the fact that df is a dataframe and not loop through and don't use the expensive .loc() operation'

    The only thing I could come up with is:

    my_list = ['col1', 'col2', ..., 'col17']
    my_dict = {}

    for i in list(df.index):
    my_dict = myClass(df.loc[i, 'name'])
    for col in my_list:
    my_dict.class_method({col: df.loc[i, col})



    but this is not really vectorizing anything... are there any secret ways around pandas vectorization that I don't know about?

    Continue reading...

Compartilhe esta Página