0

I'm doing a project and there's something I'm curious about pandas groupby(). After I grouped columns with groupby(). I just want to access alphabet column. Can I do it after grouped two columns?

macsx = new_dataset.groupby(['alphabet', 'subject'], as_index=False)['maccs']
net = macsx.apply(lambda _df : _df.iloc[0:20]).reset_index(drop=True)

net

My dataset looks like this

This is the output of the code

I want the output of my code to look like this:

A
A
A
A
.
.
.
Ş
Ş
Ş

If explained more clearly after applying the function to the grouped data set I just want to separate columns again. Now alphabet column can have 1000 rows like maccs column. I am asking if there is a way to do this?

Thank you for now.

Ayşe Hotaman
  • 17
  • 1
  • 4
  • 1
    Could you please post a minimal dataframe with a few rows and relevant columns as text along with the expected output so that it is easier to recreate the problem. It is not very clear what you are trying to do – Mortz Feb 10 '21 at 16:54
  • `I want to access one of grouped columns.` - Not real clear what you are trying to do. Can you add a *fake* DataFrame to your [mre] maybe trhe second example in [Splitting an object into groups](https://pandas.pydata.org/docs/user_guide/groupby.html#splitting-an-object-into-groups) then group it and explain what you want to do? [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Feb 10 '21 at 16:55
  • Thanks for your suggestions. I edited my question hope it will be understandable now. I shared photos but they are displayed as a link. – Ayşe Hotaman Feb 10 '21 at 17:21
  • 1
    There is too much of your code that is outside the scope of what is posted to help. But there are a handful of issues already present, such as the 'dataset' which is being passed to the method. – Boskosnitch Feb 10 '21 at 18:43
  • @Boskosnitch Thank you. I edited my question again. I reduced code part to my main question. – Ayşe Hotaman Feb 10 '21 at 19:33

1 Answers1

0

So from what I can gather, you don't need to be using a groupby, but just sort_values.

instead of;

macsx = new_dataset.groupby(['alphabet', 'subject'], as_index=False)['maccs']

try;

macsx = new_dataset.sort_values(by=['alphabet', 'subject'])['maccs']
Boskosnitch
  • 774
  • 3
  • 8