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']}) I would like to convert this to And the code I
Solution 1:
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 AB C
1 two AB C
See how df1.groupby('foo').cumcount() + 1
makes the columns:
foo columns
0one1 # First instance of "one"
1one2 # Second instance of "one"
2one3 # Third instance of "one"
3 two 14 two 25 two 3
Code to generate the above DataFrame:
demo_df = pd.DataFrame({
'foo': df1['foo'],
'columns': df1.groupby('foo').cumcount() + 1
})
Solution 2:
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 AB C
1 two AB C
Solution 3:
Another solution:
x = df1.pivot("foo", "bar", "bar")
x.columns = [f"var{i}"for i inrange(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
Solution 4:
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
Solution 5:
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!=0else"val")
.rename_axis(columns=None)
)
val val1 val2
foo
one A B C
two A B C
Post a Comment for "How To Convert Vertical Pandas Table Of 2 Columns To Horizontal Table Based On Common Id Value In Python"