将MultiIndex DataFrame转换为Series

时间:2016-12-13 06:25:38

标签: pandas series multi-index

我通过以下方式创建了一个multiIndex DataFrame:

df.set_index(['Field1', 'Field2'], inplace=True)

如果这不是multiIndex DataFrame,请告诉我如何制作一个。

我想:

  • 按索引
  • 中的相同列分组
  • 汇总每个组的计数
  • 然后以Field1和Field2作为索引
  • 返回整个系列

我该怎么做?

附加信息

我有一个如下所示的multiIndex dataFrame:

Continent     Sector                Count     
Asia          1                     4
              2                     1
Australia     1                     1
Europe        1                     1
              2                     3
              3                     2
North America 1                     1
              5                     1
South America 5                     1

如何将此作为索引为[Continent,Sector]

的系列返回

2 个答案:

答案 0 :(得分:4)

我认为您需要groupby汇总size

df = pd.DataFrame({'Field1':[1,1,1],
                   'Field2':[4,4,6],
                   'C':[7,8,9],
                   'D':[1,3,5],
                   'E':[5,3,6],
                   'F':[7,4,3]})


df.set_index(['Field1', 'Field2'], inplace=True)
print (df)
               C  D  E  F
Field1 Field2            
1      4       7  1  5  7
       4       8  3  3  4
       6       9  5  6  3

print (df.index)
MultiIndex(levels=[[1], [4, 6]],
           labels=[[0, 0, 0], [0, 0, 1]],
           names=['Field1', 'Field2'])

print (df.groupby(level=[0,1]).size())
Field1  Field2
1       4         2
        6         1
dtype: int64
print (df.groupby(level=['Field1', 'Field2']).size())
Field1  Field2
1       4         2
        6         1
dtype: int64
print (df.groupby(level=['Field1', 'Field2']).count())
               C  D  E  F
Field1 Field2            
1      4       2  2  2  2
       6       1  1  1  1

What is the difference between size and count in pandas?

通过评论编辑:

df.set_index(['Continent', 'Sector'], inplace=True)
print (df)
                      Count
Continent     Sector       
Asia          1           4
              2           1
Australia     1           1
Europe        1           1
              2           3
              3           2
North America 1           1
              5           1
South America 5           1

print (df['Count'])
Continent      Sector
Asia           1         4
               2         1
Australia      1         1
Europe         1         1
               2         3
               3         2
North America  1         1
               5         1
South America  5         1
Name: Count, dtype: int64

或者:

print (df.squeeze())
Continent      Sector
Asia           1         4
               2         1
Australia      1         1
Europe         1         1
               2         3
               3         2
North America  1         1
               5         1
South America  5         1
Name: Count, dtype: int64

全部与set_index

一起
print (df)
       Continent  Sector  Count
0           Asia       1      4
1           Asia       2      1
2      Australia       1      1
3         Europe       1      1
4         Europe       2      3
5         Europe       3      2
6  North America       1      1
7  North America       5      1
8  South America       5      1

print (df.set_index(['Continent', 'Sector'])['Count'])
Continent      Sector
Asia           1         4
               2         1
Australia      1         1
Europe         1         1
               2         3
               3         2
North America  1         1
               5         1
South America  5         1
Name: Count, dtype: int64 

答案 1 :(得分:0)

您可以像这样查询dataframe

df['count']