How to move pandas data frame multiindex column into 2 rows

Jakub Pluta

I have an issue with my data frame. I have a pandas data frame with MultiIndex Columns, so it means that in my data frame header I have tuples like ("A", 123"), ("B", 456"), ("C", 789). And I would like to create from this tuple 2 rows in the way that data frame will look like:

"A" "B" "C"
123 456 789
row row row
row row row
row row row

There are two possibilities for me. I can have a header in 2 rows, or I can have just one header but in 1 row after header, I will have my 2nd tuple element.

Can you help me with that? I tried dropping level but it didn't work.

jezrael

If need convert MultiIndex to first 2 rows use MultiIndex.to_frame with transpose and DataFrame.append, last set default columns names:

mux = pd.MultiIndex.from_tuples([("A", 123), ("B", 456), ("C", 789)])
df = pd.DataFrame(0, columns=mux, index=[0])
print (df)
    A   B   C
  123 456 789
0   0   0   0

df1 = df.columns.to_frame().T.append(df, ignore_index=True)
df1.columns = range(len(df1.columns))
print (df1)
     0    1    2
0    A    B    C
1  123  456  789
2    0    0    0

If need move only second level select it by DataFrame.iloc and last remove second level by DataFrame.droplevel:

df2 = df.columns.to_frame().T.iloc[[1]].append(df, ignore_index=True).droplevel(1, axis=1)
print (df2)
     A    B    C
0  123  456  789
1    0    0    0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create a new rows from column values of pandas data frame

From Dev

Replicating rows in a pandas data frame by a column value

From Dev

Insert column in multiindex data frame

From Dev

MultiIndex Group By in Pandas Data Frame

From Dev

How to remove just the index name and not the content in Pandas multiindex data frame

From Java

Move index values into column names in pandas Data Frame

From Dev

How to sort subset of rows in Pandas data frame

From Dev

How to get rows in pandas data frame, with maximal values in a column and keep the original index?

From Java

How to drop rows from pandas data frame that contains a particular string in a particular column?

From Dev

How to merge all rows in a pandas data frame with the same value for a specific column?

From Dev

How to get rows in pandas data frame, with maximal values in a column and keep the original index?

From Dev

pandas data frame find all rows with particular column value?

From Dev

Change value of all rows in a column of pandas data frame

From Dev

unstack multiindex dataframe to flat data frame in pandas

From Dev

unstack multiindex dataframe to flat data frame in pandas

From Dev

How to save a Data Frame column as a list? [Pandas]

From Dev

How to delete a column from a data frame with pandas?

From Dev

How to spread a column in a Pandas data frame

From Dev

Replicating rows in a pandas data frame

From Dev

How to create new pandas column based on other column of data frame?

From Dev

How to strsplit data frame column and replicate rows accordingly?

From Dev

How to combine duplicate rows of a particular column in a data frame in R

From Dev

How to combine duplicate rows of a particular column in a data frame in R

From Dev

How to get rows by a specific value of the last data frame column

From Dev

How to delete minority rows from a pandas data frame?

From Dev

How to subtract rows of one pandas data frame from another?

From Java

How to reorder indexed rows based on a list in Pandas data frame

From Dev

How to calculate differences between consecutive rows in pandas data frame?

From Dev

How to subtract rows of one pandas data frame from another?

Related Related

  1. 1

    How to create a new rows from column values of pandas data frame

  2. 2

    Replicating rows in a pandas data frame by a column value

  3. 3

    Insert column in multiindex data frame

  4. 4

    MultiIndex Group By in Pandas Data Frame

  5. 5

    How to remove just the index name and not the content in Pandas multiindex data frame

  6. 6

    Move index values into column names in pandas Data Frame

  7. 7

    How to sort subset of rows in Pandas data frame

  8. 8

    How to get rows in pandas data frame, with maximal values in a column and keep the original index?

  9. 9

    How to drop rows from pandas data frame that contains a particular string in a particular column?

  10. 10

    How to merge all rows in a pandas data frame with the same value for a specific column?

  11. 11

    How to get rows in pandas data frame, with maximal values in a column and keep the original index?

  12. 12

    pandas data frame find all rows with particular column value?

  13. 13

    Change value of all rows in a column of pandas data frame

  14. 14

    unstack multiindex dataframe to flat data frame in pandas

  15. 15

    unstack multiindex dataframe to flat data frame in pandas

  16. 16

    How to save a Data Frame column as a list? [Pandas]

  17. 17

    How to delete a column from a data frame with pandas?

  18. 18

    How to spread a column in a Pandas data frame

  19. 19

    Replicating rows in a pandas data frame

  20. 20

    How to create new pandas column based on other column of data frame?

  21. 21

    How to strsplit data frame column and replicate rows accordingly?

  22. 22

    How to combine duplicate rows of a particular column in a data frame in R

  23. 23

    How to combine duplicate rows of a particular column in a data frame in R

  24. 24

    How to get rows by a specific value of the last data frame column

  25. 25

    How to delete minority rows from a pandas data frame?

  26. 26

    How to subtract rows of one pandas data frame from another?

  27. 27

    How to reorder indexed rows based on a list in Pandas data frame

  28. 28

    How to calculate differences between consecutive rows in pandas data frame?

  29. 29

    How to subtract rows of one pandas data frame from another?

HotTag

Archive