4

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.

jpp
  • 159,742
  • 34
  • 281
  • 339
i1100362
  • 259
  • 2
  • 10

3 Answers3

2

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)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • when I use `df.xs` I get only `group ordercode 0 A 1 ` – i1100362 Oct 05 '18 at 08:55
  • @i1100362 - Sorry, you need `df = df[df['group'] == 0]` ? – jezrael Oct 05 '18 at 09:06
  • I just need to print group in a separate dataframe. Say I have 10 dataframesa and each has 100 gropus. Now in `new_df` I want to print the selective groups from each dataframe. So I need to know how to extract the group – i1100362 Oct 05 '18 at 09:08
  • @i1100362 - But why is necessary `groupby` ? I think here is not necessary, if need extract only group by filtering from comment above. Or something missing? – jezrael Oct 05 '18 at 09:09
  • this is why I have formed groups https://stackoverflow.com/questions/52475010/how-to-create-a-group-by-spliting-the-dataframe-using-python – i1100362 Oct 05 '18 at 09:13
  • 1
    @i1100362 - so need after `df['group'] = sorted(range(0, len(df)/3, 1) * 4)[0:len(df)]` ? Then `df = df[df['group'] == 0]` working fine. – jezrael Oct 05 '18 at 09:17
  • this works fine. but what if I want to program in separate file? – i1100362 Oct 05 '18 at 09:20
  • @i1100362 - Separate file? Not sure if understand. Maybe is possible post your code from linked answer, or explain more? – jezrael Oct 05 '18 at 09:21
  • Sorry. I am working in an excel file. By using Python3 I have created all these groups and saved in a separate file. now I am writing different program so I need to extract the data from my previously saved excel sheet. that's why I have posted this question. I have saved all data from the workbook in different dataframe and now I need to extract the data from each `df` – i1100362 Oct 05 '18 at 09:27
  • @i1100362 - are data confidental? – jezrael Oct 05 '18 at 09:29
  • its not confidential but the file is huge. – i1100362 Oct 05 '18 at 09:31
  • So there is list of DataFrames extracted from excel files and need filter by `group` ? Then check edited answer. – jezrael Oct 05 '18 at 09:32
  • Yes. but not checking the edited answers. I am writing a sequence (ex. production sequence)using group. which group I wanna produce first – i1100362 Oct 05 '18 at 09:34
  • @i1100362 - So `df = df[df['group'] == 0]` wotking for one DataFrame and need solution for multiple DataFrames? – jezrael Oct 05 '18 at 10:03
  • Yes. for multiple dataframes – i1100362 Oct 05 '18 at 10:37
  • @i1100362 - and multiple dataframes are in list, dictionary or something else? How are stored? – jezrael Oct 05 '18 at 10:37
  • Shall I post as a new question ?? – i1100362 Oct 05 '18 at 10:38
  • @i1100362 - Sure, maybe also help [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – jezrael Oct 05 '18 at 10:39
  • https://stackoverflow.com/questions/52663926/selecting-data-from-multiple-dataframes – i1100362 Oct 05 '18 at 10:49
2
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
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
2

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
jpp
  • 159,742
  • 34
  • 281
  • 339
  • I have the group. Now I want to access the data from the group. I have 10 data frames and each has 100 groups. so I want to select one group(using its group number) from each dataframe. So I need to know how to get particular group from my df – i1100362 Oct 05 '18 at 09:00
  • `I have formed the group already.` What does that mean exactly? To me, it just means you've done `gb = df.groupby('A')`. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for how you can improve your question. – jpp Oct 05 '18 at 09:00
  • yes. Now how can I get data from my group using group number – i1100362 Oct 05 '18 at 09:02
  • 1
    @i1100362, As in my answer, feed the key to `get_group`, e.g. `gb[gb.obj.columns].get_group(0)` if your key is `0`. – jpp Oct 05 '18 at 09:02