vinay kumar
vinay kumar

Reputation: 71

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

Upvotes: 7

Views: 1663

Answers (6)

sammywemmy
sammywemmy

Reputation: 28709

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

Upvotes: 0

Scott Boston
Scott Boston

Reputation: 153510

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

Upvotes: 2

Abdel
Abdel

Reputation: 159

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

Upvotes: 0

Vetedde
Vetedde

Reputation: 185

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')

Upvotes: -1

Henry Ecker
Henry Ecker

Reputation: 35686

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
})

Upvotes: 6

Andrej Kesely
Andrej Kesely

Reputation: 195553

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

Upvotes: 1

Related Questions