0

I have data that looks like the following Pandas datafame:

residue  aa   residue
L37      N    2.0
L37      A    3.0
L37      M    4.0
L37      R    5.0
L38      N    12.0
L39      A    13.0
L40      M    14.0
L41      R    15.0
...

I am trying to re-orient the data such that it looks more like a 2D matrix:

     N     A     M     R
L37  2.0   3.0   4.0   5.0
L38  12.0  13.0  14.0  15.0
...

The only way I can think to to this is to create a 2D array a loop over each line. It seems like Pandas might have a better way though, so I am looking for any pointers that someone could help me with.

Can anyone point me in the correct direction for using Pandas to convert the "row-based" data to "matrix-based" data?

Brett
  • 11,637
  • 34
  • 127
  • 213
  • Just rename the second `residue` column and use `df.pivot_table('residue.1', 'aa', 'residue', fill_value=0)` or if you can't rename the column, use `df.pivot_table(df.columns[2], df.columns[0], df.columns[1], fill_value=0)` – Corralien Jun 07 '22 at 19:19
  • Or maybe `df.pivot(index='residue', columns='aa', values='residue.1')` –  Jun 07 '22 at 19:20

0 Answers0