简体   繁体   中英

Multi-column, multi-row pandas data frame

I am trying to pass the row with index 0 to a multi-column df. That is, I want to have the same df but with index-0 row in bold, above the vertical line. I have tried to do this without success. Help would be appreciated.

enter image description here

If I understand your problem correctly you could try this to transfer your first row from the original df to the head of the new one.

import pandas as pd

df = pd.DataFrame(data=
                  {'Name': ["Peter", "Andy", "Bob", "Lisa"],
                   'Age': [50, 40, 34, 39],
                   'Nationality': ["USA", "Australian", "Candian", "Mexican"]}
                  )

dict = {}
for titel in df.iloc[0]:
    dict[titel] = ["TestValue", "Testvalue"]
    
print(dict)
dfN = pd.DataFrame(data=dict) # new Dict

Pandas dataframe can have multiple column headers for columns or rows.

Make a dataframe.

import pandas as pd
import numpy as np

df = pd.DataFrame(
    data=np.random.randint(
        0, 10, (6,4)),
    columns=["a", "b", "c", "d"])

Insert a second column index.

df.columns = pd.MultiIndex.from_tuples(
    zip(['A', 'B','C', 'D'], 
        df.columns))

output

   A  B  C  D
   a  b  c  d
0  4  9  5  1
1  7  5  3  7
2  1  1  5  1
3  8  3  8  3
4  3  8  8  2
5  5  2  3  5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM