Issue
I have a pandas dataframe that is pivoted. I do not know how to change column names so that I can continue working with the pivoted dataframe in a natural way. Below is a minimal working example.
df = pd.DataFrame({'foo': ['one', 'one', 'one', 'two', 'two',
'two'],
'bar': ['A', 'B', 'C', 'A', 'B', 'C'],
'baz': [1, 2, 3, 4, 5, 6],
'zoo': ['x', 'y', 'z', 'q', 'w', 't']})
After pivoting with
df.pivot(index='foo', columns='bar', values=['baz', 'zoo'])
the output is:
baz zoo
bar A B C A B C
foo
one 1 2 3 x y z
two 4 5 6 q w t
What would be the following step to do in order to obtain below output?
A_baz B_baz C_baz A_zoo B_zoo C_zoo
one 1 2 3 x y z
two 4 5 6 q w t
Thanks a lot!
Solution
Use f-string
s with list comprehension:
#python 3.6+
df.columns = [f'{j}_{i}' for i, j in df.columns]
#lower python versions
#df.columns = ['{}_{}'.format(j, i) for i, j in df.columns]
print (df)
A_baz B_baz C_baz A_zoo B_zoo C_zoo
foo
one 1 2 3 x y z
two 4 5 6 q w t
Or DataFrame.swaplevel
with map
and join
:
df = df.pivot(index='foo', columns='bar', values=['baz', 'zoo']).swaplevel(0,1,axis=1)
df.columns = df.columns.map('_'.join)
print (df)
A_baz B_baz C_baz A_zoo B_zoo C_zoo
foo
one 1 2 3 x y z
two 4 5 6 q w t
Answered By - jezrael
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.