如何通过分组索引访问Pandas groupby数据框?

4

按照这个例子,我可以创建一个简单的数据框并进行分组

import pandas as pd

# Create a sample data frame
df = pd.DataFrame({'A': ['foo', 'foo', 'foo', 'bar', 'bar'],
                   'B': range(5), 'C': range(5)})

# group by 'A' and sum 'B'
gf = df.groupby('A').agg({'B': 'sum'})

结果是分组数据框gf。
    B
A   
bar 7
foo 3

我想通过分组索引访问gf。类似于...

gf['foo'] returns 3 
gf['bar'] returns 7

我也希望通过分组索引绘制图表。 类似于...
gf.plot('A', 'B') such that  x=['foo','bar'], y=[3,7]
3个回答

6
gf.reset_index(level=0, inplace=True)

gf[gf.A == 'bar']

返回值:

     A  B
0  bar  7

情节:

import matplotlib.pyplot as plt

plt.bar(gf.A, gf.B)

0

在撰写本文时,即将被弃用,有一种方法可以访问分组索引而不使用reset_index。这是一个非常糟糕的过程,不应该使用,但是下面是具体步骤:

df_counts.index.to_series().str[0]

返回

A
bar    b
foo    f
Name: A, dtype: object

这将给你与重置数据框并按该列索引相同的结果。具体来说,在str之后的索引通过索引列表,例如['a','b']

要访问此值的b中的值,对于此值的a,您可以只需:

gf[gf.index.to_series().str[0] == 'b']['B']

A
bar    7
Name: B, dtype: int64

如果你确定它是唯一的,就强制转换为整数:

int(gf[gf.index.to_series().str[0] == 'b']['B'])

7

请记住,这是执行此查询的可怕方式,正如我已经发现的那样,你应该非常遵循@LN_P的答案,它简单地重置了无意义地创建的索引,使你能够像平常一样访问列。


0

这个怎么样:

import matplotlib.pyplot as plt

for k in gf['B'].index:
    print "{}: {}".format(k, gf['B'].loc[k])

plt.bar(gf['B'].index, map(lambda i: gf['B'].loc[i], gf['B'].index))
plt.show()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接