将值插入多级索引数据框

时间:2019-08-08 04:39:59

标签: python pandas dataframe

我已经创建了一个数据框,如下所示:

idx = pd.MultiIndex.from_product([['batch1', 'batch2','batch3', 'batch4', 'batch5'], ['quiz1', 'quiz2']])
cols=['noofpresent', 'lesserthan50', 'between50and60', 'between60and70', 'between70and80', 'greaterthan80']
statdf = pd.DataFrame('-', idx, cols)
statdf

Dataframe Created (statdf)

statdf.loc['quiz1', 'noofpresent'] = qdf1.b4ispresent.count()
statdf.loc['quiz2', 'noofpresent'] = qdf2.b4ispresent.count()
statdf.noopresent = qdf1.b4ispresent.count()
statdf.noopresent = qdf2.b4ispresent.count()
statdf

然后我做了一些计算。我现在想在“ batch4 ”,“ quiz1”和“ quiz2”的“ noofpresent”列中分别附加数字“ 50”和“ 53”的具体计算。而是发生了...

dataframe after insertion

如何将数据插入正确的位置?

1 个答案:

答案 0 :(得分:0)

您可以像这样将其编入索引。

statdf.loc['batch4','quiz1']['noofpresent'] = qdf1.b4ispresent.count() statdf.loc['batch4','quiz2']['noofpresent'] =qdf2.b4ispresent.count()