# 通过groupby功能选择群组[英] Selecting groups fromed by groupby function

### 问题描述

``` df1

group     ordercode      quantity
0            A             1
B             3
1            C             1
E             2
D             1
```

in:获取组0 出局:

```   ordercode      quantity
A             1
B             3
```

``` group     ordercode      quantity
0            A             1
B             3
```

## 推荐答案

```In [131]: df.loc[pd.IndexSlice[0,:]]
Out[131]:
quantity
ordercode
A                 1
B                 3
```

```In [130]: df.loc[pd.IndexSlice[0,:], :]
Out[130]:
quantity
group ordercode
0.0   A                 1
B                 3
```

## 其他推荐答案

### 问题描述

My dataframe:

``` df1

group     ordercode      quantity
0            A             1
B             3
1            C             1
E             2
D             1
```

I have formed each group bygroupby function.

I need to extract the data by using group number.

My desired ouput.

In:get group 0 out:

```   ordercode      quantity
A             1
B             3
```

or

``` group     ordercode      quantity
0            A             1
B             3
```

any suggestion would be appreciated.

## 推荐答案

Use DataFrame.xs, also is possible use parameter drop_level=False:

```#if need remove original level

df1 = df.xs(0)
print (df1)
quantity
ordercode
A                 1
B                 3

#if avoid remove original level
df1 = df.xs(0, drop_level=False)
print (df1)
quantity
group ordercode
0     A                 1
B                 3
```

EDIT:

```dfs = [df1, df2, df3]
dfs = [x[x['group'] == 0] for x in dfs]
print (dfs)
```

## 其他推荐答案

```In [131]: df.loc[pd.IndexSlice[0,:]]
Out[131]:
quantity
ordercode
A                 1
B                 3
```

or

```In [130]: df.loc[pd.IndexSlice[0,:], :]
Out[130]:
quantity
group ordercode
0.0   A                 1
B                 3
```

## 其他推荐答案

You can use GroupBy.get_group after specifying columns. Here's a demo:

```df = pd.DataFrame({'A': ['foo', 'bar'] * 3,
'B': np.random.rand(6),
'C': np.arange(6)})

gb = df.groupby('A')

print(gb[gb.obj.columns].get_group('bar'))

A         B  C
1  bar  0.523248  1
3  bar  0.575946  3
5  bar  0.318569  5
```