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.
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.
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.
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.
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.
Yes! It's called transposition.
a.set_index(['one', 'two']).T
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With