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

341
Vistas
How to convert vertical pandas table of 2 columns to horizontal table based on common ID value in python
df1 = pd.DataFrame({'foo': ['one', 'one', 'one', 'two', 'two', 'two'],
                    'bar': ['A', 'B', 'C', 'A', 'B', 'C']})
foo bar
0 one A
1 one B
2 one C
3 two A
4 two B
5 two C

I would like to convert this to

foo val1 val2 val3
One A B C
Two A B C

And the code I tried is:

pd.pivot_table(df1,index='foo',aggfunc=['first'])

But the above code is returning only the first value

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

0

We can enumerate groups with groupby cumcount and use those as the pivot columns then add_prefix to the numerical values and reset_index to return the 'foo' values to the columns:

new_df = (
    df1.pivot_table(index='foo',
                    columns=df1.groupby('foo').cumcount() + 1,
                    values='bar', 
                    aggfunc='first')
        .add_prefix('val')
        .reset_index()
)
   foo val1 val2 val3
0  one    A    B    C
1  two    A    B    C

See how df1.groupby('foo').cumcount() + 1 makes the columns:

   foo  columns
0  one        1  # First instance of "one"
1  one        2  # Second instance of "one"
2  one        3  # Third instance of "one"
3  two        1
4  two        2
5  two        3

Code to generate the above DataFrame:

demo_df = pd.DataFrame({
    'foo': df1['foo'],
    'columns': df1.groupby('foo').cumcount() + 1
})
over 4 years ago · Santiago Trujillo Denunciar

0

Another solution:

x = df1.pivot("foo", "bar", "bar")
x.columns = [f"var{i}" for i in range(1, len(x.columns) + 1)]
x = x.reset_index()
print(x)

Prints:

   foo var1 var2 var3
0  one    A    B    C
1  two    A    B    C
over 4 years ago · Santiago Trujillo Denunciar

0

Try:

df1.groupby([df1.groupby('foo').cumcount() + 1,
             'foo']).first()['bar'].unstack(0).add_prefix('val').reset_index()

Output:

   foo val1 val2 val3
0  one    A    B    C
1  two    A    B    C
over 4 years ago · Santiago Trujillo Denunciar

0

df1 = pd.DataFrame({'foo': ['one', 'one', 'one', 'two', 'two', 'two'],
                    'bar': ['A', 'B', 'C', 'A', 'B', 'C']})
df1['i'] = df1['bar'].apply(lambda x: "val "+x)
pd.pivot(df1,index='foo',columns='i',values='bar')
over 4 years ago · Santiago Trujillo Denunciar

0

pd.pivot_table(df1,index='foo',columns=df1.groupby(df1.foo).cumcount()+1,
               values='bar',aggfunc=(lambda x:min(x)), dropna=True).add_prefix('val')

output:

    val1  val2  val 3
foo         
one   A     B     C
two   A     B     C
over 4 years ago · Santiago Trujillo Denunciar

0

Taking a cue from @HenryEcker's solution, a combination of groupby and pivot can get the result:

(df1.assign(counter = lambda df: df.groupby('foo').cumcount())
    .pivot('foo', 'counter', 'bar')
    .rename(columns = lambda col: f"val{col}" if col!=0 else "val")
    .rename_axis(columns=None)
)
    val val1 val2
foo              
one   A    B    C
two   A    B    C
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