Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas setting multi-index on rows, then transposing to columns

If I have a simple dataframe:

print(a)

  one  two three
0   A    1     a
1   A    2     b
2   B    1     c
3   B    2     d
4   C    1     e
5   C    2     f

I can easily create a multi-index on the rows by issuing:

a.set_index(['one', 'two'])

        three
one two      
A   1       a
    2       b
B   1       c
    2       d
C   1       e
    2       f

Is there a similarly easy way to create a multi-index on the columns?

I'd like to end up with:

    one A       B       C   
    two 1   2   1   2   1   2
    0   a   b   c   d   e   f

In this case, it would be pretty simple to create the row multi-index and then transpose it, but in other examples, I'll be wanting to create a multi-index on both the rows and columns.

like image 588
sheridp Avatar asked Aug 16 '16 17:08

sheridp


People also ask

How do I change multiple index to columns in pandas?

We can easily convert the multi-level index into the column by the reset_index() method. DataFrame. reset_index() is used to reset the index to default and make the index a column of the dataframe. Step 1: Creating a multi-index dataframe.

How do you transpose rows to columns in pandas?

Pandas DataFrame: transpose() functionThe transpose() function is used to transpose index and columns. Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa. If True, the underlying data is copied. Otherwise (default), no copy is made if possible.

How do I change multiple indexes to single index in pandas?

Output: Now, the dataframe has Hierarchical Indexing or multi-indexing. To revert the index of the dataframe from multi-index to a single index using the Pandas inbuilt function reset_index(). Returns: (Data Frame or None) DataFrame with the new index or None if inplace=True.

How do I rearrange index after dropping rows pandas?

Import the Pandas module. Create a DataFrame. Drop some rows from the DataFrame using the drop() method. Reset the index of the DataFrame using the reset_index() method.


2 Answers

Yes! It's called transposition.

a.set_index(['one', 'two']).T

enter image description here


Let's borrow from @ragesz's post because they used a much better example to demonstrate with.

df = pd.DataFrame({'a':['foo_0', 'bar_0', 1, 2, 3], 'b':['foo_0', 'bar_1', 11, 12, 13],
    'c':['foo_1', 'bar_0', 21, 22, 23], 'd':['foo_1', 'bar_1', 31, 32, 33]})

df.T.set_index([0, 1]).T

enter image description here

like image 184
piRSquared Avatar answered Oct 09 '22 18:10

piRSquared


You could use pivot_table followed by a series of manipulations on the dataframe to get the desired form:

df_pivot = pd.pivot_table(df, index=['one', 'two'], values='three', aggfunc=np.sum)

def rename_duplicates(old_list):    # Replace duplicates in the index with an empty string
    seen = {}
    for x in old_list:
        if x in seen:
            seen[x] += 1
            yield " " 
        else:
            seen[x] = 0
            yield x

col_group = df_pivot.unstack().stack().reset_index(level=-1)
col_group.index = rename_duplicates(col_group.index.tolist())
col_group.index.name = df_pivot.index.names[0]
col_group.T

one  A     B     C   
two  1  2  1  2  1  2
0    a  b  c  d  e  f
like image 40
Nickil Maveli Avatar answered Oct 09 '22 18:10

Nickil Maveli