Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 序列中的数据帧,索引值为行/列_Python_Pandas_Dataframe - Fatal编程技术网

Python 序列中的数据帧,索引值为行/列

Python 序列中的数据帧,索引值为行/列,python,pandas,dataframe,Python,Pandas,Dataframe,在数据帧上进行groupby和count之后,我有一个如下序列: gender_neutral_death gender ascended Fem 103 Mal 69 burned by a tower of flame Fem 5 Mal

在数据帧上进行
groupby
count
之后,我有一个如下序列:

gender_neutral_death            gender
ascended                        Fem       103
                                Mal        69
burned by a tower of flame      Fem         5
                                Mal         1
burned by molten lava           Fem         4
                                         ... 
went to heaven prematurely      Mal         1
zapped themselves with a spell  Fem        10
                                Mal         7
zapped themselves with a wand   Fem         8
                                Mal        10
现在,我想使用“性别”来构建两列“Mal”和“Fem”,我的案例是:

gender_neutral_death        Mal    Fem
ascended                     69    103
burned by a tower of flame    1      5
[...]
to_frame()
方法几乎满足了我的需求:

应用
unstack
使我更接近,但不是完全:


如何实现上面示例中给出的结果?

我假设groupby/count操作的结果是一个系列

在这种情况下,
unstack()
函数接受一个级别(1或“性别”):

s.unstack(level='gender').fillna(0).astype(int)
按性别使用
。取消堆叠(级别=1)