Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

339
Views
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 answers
Answer question

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 Report

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 Report

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 Report

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!