Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse a 2-dimensional table (DataFrame) into a 1 dimensional list using Pandas?

I am looking in Python/Pandas for a tip that reverses a 2-dimension table into 1 dimensional list.

I usually leverage an Excel function to do it, but I believe that there is a smart Python way to do it.

Step

The steps of "2-dimension table into 1 dimension list"

More details of the Excel way: http://www.extendoffice.com/documents/excel/2461-excel-reverse-pivot-table.html

like image 683
Ning Chen Avatar asked Jan 04 '15 10:01

Ning Chen


People also ask

How do I reverse data in a pandas DataFrame?

Using iloc() function to Reverse Row Reversing the rows of a data frame in pandas can be done in python by invoking the iloc() function.

How do you reverse a pivot table in Python?

melt() function to reverse our pivoted data. The . melt() function comes in handy when you need to reshape or unpivot a DataFrame.

What is the reverse of melt in pandas?

We can also do the reverse of the melt operation which is also called as Pivoting. In Pivoting or Reverse Melting, we convert a column with multiple values into several columns of their own. The pivot() method on the dataframe takes two main arguments index and columns .


2 Answers

This type of operation could also be done using pd.melt, which unpivots a DataFrame.

If the DataFrame df looks like this:

  row labels  Tue  Wed  Thu  Sat  Sun  Fri  Mon
0      Apple   21   39   24   27   37   46   42
1     Banana   32   50   48   35   21   27   22
2       Pear   37   20   45   45   31   50   32

Then we select the row_labels column to be our id_var and the rest of the columns to be our values (value_vars). We can even choose the new names for the columns at the same time:

>>> pd.melt(df, 
            id_vars='row labels', 
            value_vars=list(df.columns[1:]), # list of days of the week
            var_name='Column', 
            value_name='Sum of Value')

   row labels   Column   Sum of Value
0       Apple      Tue             21
1      Banana      Tue             32
2        Pear      Tue             37
3       Apple      Wed             39
4      Banana      Wed             50
5        Pear      Wed             20
...

The value_vars are stacked below each other: if the column values need to be in a particular order it will be necessary to sort the columns after melting.

like image 108
Alex Riley Avatar answered Sep 20 '22 22:09

Alex Riley


This should do the trick:

table = [
            ["Lables", "A", "B", "C"],
            ["X", 1, 2, 3],
            ["Y", 4, 5, 6],
            ["Z", 7, 8, 9]
        ]

new_table = [["Row", "Column", "Data"]]
for line in table[1:]:
    for name, cell in zip(table[0], line)[1:]:
        new_line = [line[0], name, cell]
        new_table.append(new_line)

The output is:

[
    ['Row', 'Column', 'Data'],
    ['X', 'A', 1],
    ['X', 'B', 2],
    ['X', 'C', 3],
    ['Y', 'A', 4],
    ['Y', 'B', 5],
    ['Y', 'C', 6],
    ['Z', 'A', 7],
    ['Z', 'B', 8],
    ['Z', 'C', 9]
]
like image 20
tmr232 Avatar answered Sep 16 '22 22:09

tmr232