1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Is there a function to filter a dataframe by a column value and produce a dictionary?

Discussão em 'Python' iniciado por Stack, Setembro 16, 2025.

  1. Stack

    Stack Membro Participativo

    In python pandas, I'm wondering if there is a builtin function that does the same as df_to_dict below. Speed is of the essence, as my dataframe can have thousands of rows. Basically return a dictionary with keys being the set of values in df[column] and the values being the rest of the dataframe corresponding to where df[column] == value.


    def df_to_dict(df, column):
    def _filter(df, column, value):
    df_return = df[df[column] == value]
    del df_return[column]
    return df_return

    return {value: _filter(df, column, value) for value in set(df[column].values)}

    Continue reading...

Compartilhe esta Página