猿问

将值插入多级索引数据帧

我创建了一个数据框,如图所示:


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

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”的具体计算。但相反,这发生了......

http://img.mukewang.com/6268f8a20001b01108670395.jpg

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



陪伴而非守候
浏览 114回答 1
1回答

Smart猫小萌

你可以像这样索引它。statdf.loc['batch4','quiz1']['noofpresent'] = qdf1.b4ispresent.count() statdf.loc['batch4','quiz2']['noofpresent'] =qdf2.b4ispresent.count()
随时随地看视频慕课网APP

相关分类

Python
我要回答