将单元格值设置在由MultiIndex创建的标题下-Pandas

时间:2018-12-03 08:45:49

标签: pandas

enter image description here

我希望做的是在说明状态列下面的单元格内填充数据。

这就是我用来创建图像中显示的数据的方式。

header = pd.MultiIndex.from_product([['Functional Checks'],['Description','Status']])
data_frame = pd.DataFrame(index=[],columns=header)

1 个答案:

答案 0 :(得分:0)

您需要先指定index,然后再使用DataFrame.at

header = pd.MultiIndex.from_product([['Functional Checks'],['Description','Status']])
data_frame = pd.DataFrame(index=range(5),columns=header)

data_frame.at[1, ('Functional Checks', 'Status')] = 'status1'
print (data_frame)
  Functional Checks         
        Description   Status
0               NaN      NaN
1               NaN  status1
2               NaN      NaN
3               NaN      NaN
4               NaN      NaN

更好的是由类似{p>的构造函数创建的DtaFrame

vals = [['a', 'status1'], ['b', 'status2']]
header = pd.MultiIndex.from_product([['Functional Checks'],['Description','Status']])
data_frame = pd.DataFrame(columns=header, data=vals)

print (data_frame)
  Functional Checks         
        Description   Status
0                 a  status1
1                 b  status2