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] Remove items within pandas DataFrameGroupBy groups

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

  1. Stack

    Stack Membro Participativo

    I have a dataframe df made up of n columns which are groups and one, "data". This dataframe is then grouped on the n group columns.

    df = pd.DataFrame(data={"g0": ["foo", "foo", "bar", "bar"],
    "g1": ["baz", "baz", "baz", "qux"],
    ...,
    "gn": [...],
    "data": [0.1, 0.3, 0.4, 0.2]},
    index=["a", "b", "c", "d"])
    groups = df.groupby(by=["g0", "g1", ..., "gn"], sort=False)


    Then I have a list idx_kept which includes only some of the original dataframe indices e.g. idx_kept = ["a", "b", "d"]. Is there a way to filter groups and keep only the data which initiall had the indices in idx_kept? My understanding of DataFrameGroupBy.filter is that it is not appropriate in that case as it uses an aggregate function and remove whole goups.

    I could filter df directly to get a df_filtered and do a groups_filtered=df_filtered.groupby(by=["g0", "g1", ..., "gn"], sort=False). However, In my process, I need both groups and groups_filtered so my goal is to avoid a second grouby to save some time. Is there an elegant/fast way to achieve that?

    Continue reading...

Compartilhe esta Página