0

I currently got this dataframe: original dataframe However I would like to obtain a dataframe (not containing the 't') from this which looks like this (considering the index): The index we want for our original dataframe

This of course is done easily when using .groupby().agg(), but the thing is that I don't got a simple aggregation function such as 'max' or 'mean', that I would like to use. Hence my question is: 'Is it possible to group by a dataframe with a customized aggregation function and without using SQL? If so, please let me know!'

I would love to get some help!

Simplified code example explaining my question:

df_example = 

    C D E
A B
1 2 5 8 9
  3 7 9 3
2 4 9 5 5
  6 1 4 5

We would like to obtain:

df_example_groupedby_A_only_aggregating_with_custom_function = 
  Z_custom 
A
1 33       
2 34

The values in Z_custom are obtained by using the custom aggregation function which uses the values in columns [C,D,E] from df_example.
Luke_0162
  • 1
  • 2
  • It's not entirely clear what your desired outcome is. Please have a look at [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and add your sample input and expected output in the text of your question, not as a picture or link. It looks like you may just want [a MultiIndex](https://pandas.pydata.org/pandas-docs/stable/user_guide/advanced.html) in your existing dataframe? – G. Anderson Mar 22 '21 at 23:19
  • @G.Anderson, first things first: thanks a lot for your response! However, It's not just a multiIndex that I would like to obtain from my original dataframe. I would like to obtain a dataframe which is grouped by [user,trial] and using some of the columns from the original dataframe in a certain custom function I would like to obtain values from that indexed by [user,trial] (so not with 't' in it). Hopefully, this makes it a bit clearer? – Luke_0162 Mar 22 '21 at 23:24
  • @G.Anderson I already looked up if there were other ways to do this, but without success: I only got answers which used queries etc. Hence, I hope you might know if this is even possible and how? – Luke_0162 Mar 22 '21 at 23:30
  • Unfortunately it's still not clear what your actual expected output is, or your aggregation method. You can [edit] your original question to include a [mcve] rather than a picture and that would help a lot. Otherwise all we can really do is point you to posts like [python pandas custom agg function](https://stackoverflow.com/questions/14246817/python-pandas-custom-agg-function) – G. Anderson Mar 22 '21 at 23:33
  • @G.Anderson, to be honest I haven't mastered editing questions like this yet. Nonetheless, hopefully this is clear enough! Also, feel free to give me constructive criticism to help me better my way of asking questions on stackoverflow, because I haven't done that many times yet! – Luke_0162 Mar 23 '21 at 00:01

0 Answers0