如何使用Pandas创建具有多层行标题(索引)的表

时间:2015-04-09 01:33:12

标签: python pandas

我有以下数据:

# colh1 rh1 rh2 rh3/up rh4/down
AddaVax ID LV 29 18
AddaVax ID SP 16 13
AddaVax ID LN 61 73
ADX ID LV 11 14
ADX IP LV 160 88
ADX ID SP 14 13
ADX IP SP 346 129
ADX ID LN 25 25

我想做的是制作一张看起来像这样的桌子 (稍后用文字或Excel文件写):

enter image description here

实际数据包含2列以上但行数 总是固定的(即10行)。

我坚持使用以下代码:

import pandas as pd
from collections import defaultdict

dod = defaultdict(dict)
with open("mediate.txt", 'r') as tsvfile:
    tabreader = csv.reader(tsvfile, delimiter=' ')
    for row in tabreader:
        if "#" in row[0]: continue
        colh1, rh1, rh2, rhup, rhdown = row
        dod["colh1"] = colh1
        dod["rh1"] = rh1
        dod["rh2"] = rh2
        dod["rhup"] = rhup
        dod["rhdown"] = rhdown

该怎么做?

1 个答案:

答案 0 :(得分:2)

刚刚使用熊猫:

import pandas as pd

df = pd.read_csv('mediate.txt', sep='\t')  # or sep=',' if comma delimited.
df.rename(columns={'rh3/up': 'Up', 'rh4/down': 'Down'}, inplace=True)
result = df.pivot_table(values=['Up', 'Down'], 
                        columns='colh1', 
                        index=['rh1', 'rh2']).stack(0)  # Stack Up/Down
>>> result
colh1         ADX  AddaVax
rh1 rh2                   
ID  LN  Up     25       61
        Down   25       73
    LV  Up     11       29
        Down   14       18
    SP  Up     14       16
        Down   13       13
IP  LV  Up    160      NaN
        Down   88      NaN
    SP  Up    346      NaN
        Down  129      NaN