如何在 pandas 多索引数据框中插入一行? [英] How to insert a row in a Pandas multiindex dataframe?
本文介绍了如何在 pandas 多索引数据框中插入一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有多索引(Reg,Type,Part,IsExpired)的Pandas数据框-
I have a Pandas dataframe with a multiindex (Reg, Type, Part, IsExpired)-
Reg Type Part IsExpired Quantity
APAC Disk A False 10
True 12
EMEA Disk A False 22
EMEA Disk B False 13
True 17
我想确保IsExpired的每个(Reg,Type,Part)元组都具有True和False.例如.我想为(EMEA,Disk,A,True)-
I want to make sure that every (Reg, Type, Part) tuple has True and False for IsExpired. E.g. I'd like to insert a row for (EMEA, Disk, A, True)-
Reg Type Part IsExpired Quantity
APAC Disk A False 10
True 12
EMEA Disk A False 22
True 0 <-- inserted row
EMEA Disk B False 13
True 17
推荐答案
您可以 unstack
,然后也许将此列保留为有意义吗?否则,堆栈退回:
Perhaps it makes sense to keep this as a column? Otherwise stack it back:
In [14]: df2.unstack().fillna(0).stack()
Out[14]:
Quantity
Reg Type Part IsExpired
APAC Disk A False 10
True 12
EMEA Disk A False 22
True 0
B False 13
True 17
这篇关于如何在 pandas 多索引数据框中插入一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文