简体   繁体   中英

pandas: how to use column name to pivot table

I have a table looks like:

name    A    B    C
Tom     1    2    3
Jack    2    5    9
Joe     4    7    1

I want to pivot this table into a new table with only 3 columns:

name    letter    value
Tom     A         1
Tom     B         2
Tom     C         3
Jack    A         2
Jack    B         5
Jack    C         9
Joe     A         4
Joe     B         7
Joe     C         1

what I am doing now is a for loop

temp = []
for c in ['A','B',C']:
    #create new dataframe for letter
    #append new frame to temp

return pd.concat(temp)

Does anyone know an elegant way of doing this?

Thanks!

You are looking for melt

df.melt('name')
Out[5]: 
   name variable  value
0   Tom        A      1
1  Jack        A      2
2   Joe        A      4
3   Tom        B      2
4  Jack        B      5
5   Joe        B      7
6   Tom        C      3
7  Jack        C      9
8   Joe        C      1

Edit from Scott Boston

Let's name that 'variable' column with var_name paramater as OP indicates:

df.melt(id_vars='name', var_name='letter')

   name letter  value
0   Tom      A      1
1  Jack      A      2
2   Joe      A      4
3   Tom      B      2
4  Jack      B      5
5   Joe      B      7
6   Tom      C      3
7  Jack      C      9
8   Joe      C      1

use stack() , after setting Name as an index:

In [397]: df.set_index(df.name)[['A','B','C']].stack()
Out[397]: 
name   
Tom   A    1
      B    2
      C    3
Jack  A    2
      B    5
      C    9
Joe   A    4
      B    7
      C    1
dtype: int64

If you want three data columns, just do :

In [412]: u=df.set_index(df.name)[['A','B','C']].stack().reset_index()

In [413]: u.columns=['name','letter','value']

In [414]: u
Out[414]: 
   name letter  value
0   Tom      A      1
1   Tom      B      2
2   Tom      C      3
3  Jack      A      2
4  Jack      B      5
5  Jack      C      9
6   Joe      A      4
7   Joe      B      7
8   Joe      C      1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM