简体   繁体   English

多列多行 pandas 数据框

[英]Multi-column, multi-row pandas data frame

I am trying to pass the row with index 0 to a multi-column df.我正在尝试将索引为 0 的行传递给多列 df。 That is, I want to have the same df but with index-0 row in bold, above the vertical line.也就是说,我想拥有相同的 df 但在垂直线上方以粗体显示 index-0 行。 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.如果我正确理解您的问题,您可以尝试将第一行从原始 df 转移到新的开头。

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. Pandas dataframe 可以有多个列标题用于列或行。

Make a dataframe.制作 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 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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