Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
Include indices in Pandas groupby results

With Pandas groupby, I can do things like this:

>>> df = pd.DataFrame(
...     {
...         "A": ["foo", "bar", "bar", "foo", "bar"],
...         "B": ["one", "two", "three", "four", "five"],
...     }
... )
>>> print(df)
     A      B
0  foo    one
1  bar    two
2  bar  three
3  foo   four
4  bar   five
>>> print(df.groupby('A')['B'].unique())
A
bar    [two, three, five]
foo           [one, four]
Name: B, dtype: object

What I am looking for is output that produces a list of indices instead of a list of column B:

A
bar    [1, 2, 4]
foo    [0, 3]

However, groupby('A').index.unique() doesn't work. What syntax would provide me the output I'm after? I'd be more than happy to do this in some other way than with groupby, although I do need to group by two columns in my real application.

over 4 years ago · Santiago Trujillo
4 Respuestas
Responde la pregunta

0

Use df.reset_index with Groupby.Series.unique

In [530]: df.reset_index().groupby('A')['index'].unique()
Out[530]: 
A
bar    [1, 2, 4]
foo       [0, 3]
Name: index, dtype: object

OR:

In [533]: df.reset_index().groupby('A')['index'].agg(list)
Out[533]: 
A
bar    [1, 2, 4]
foo       [0, 3]
Name: index, dtype: object
over 4 years ago · Santiago Trujillo Denunciar

0

You do not necessarily need to have a label in groupby, you can use a grouping object.

This enables things like:

df.index.to_series().groupby(df['A']).unique()

output:

A
bar    [1, 2, 4]
foo       [0, 3]
dtype: object
getting the indices of the unique B values:
df[~df[['A', 'B']].duplicated()].index.to_series().groupby(df['A']).unique()
over 4 years ago · Santiago Trujillo Denunciar

0

One intuitive way is to add a line for defining a new column as index, and you can keep using the same code as you wrote.

df['index'] = df.index
df.groupby('A')['index'].unique()

Result:

enter image description here

over 4 years ago · Santiago Trujillo Denunciar

0

If you want indices of unique values in 'B', as opposed to unique indices, then you can do

df.reset_index().groupby('A').apply(lambda g: g.drop_duplicates(['B'])['index'].tolist())

it is different from @Mayank and @mozway answers when applied to a slightly modified example df:

df = pd.DataFrame(
    {
        "A": ["foo", "bar", "bar", "foo", "bar", "foo"],
        "B": ["one", "two", "three", "four", "five", "one"],
    }
)

My answer would return

A
bar    [1, 2, 4]
foo       [0, 3]
dtype: object

whereas @Mayank and @mozway would return

A
bar    [1, 2, 4]
foo    [0, 3, 5]
Name: index, dtype: object
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda