3

I want to be able to concat dataframe results to memory as they go through a function and end up with a whole new dataframe with just the results. How do I do this without having a dataframe all ready created before the function? For example:

import pandas as pd
import numpy as np   

rand_df = pd.DataFrame({'A': [ 'x','x','y','y','z','z','z'],'B': np.random.randn(7)})

    def myFuncOnDF(df, row):
        df = df.groupby(['A']).get_group(row).describe()

    myFuncOnDF(rand_df, 'x')
    myFuncOnDF(rand_df, 'y')
    myFuncOnDF(rand_df, 'z')

How would I concat the results of myFuncOnDF() to a new dataframe that doesn't exist yet?

1 Answer 1

5

Not really sure what you expected but groupby and describeaccomplishes the same thing

rand_df.groupby('A').B.describe().unstack()

   count      mean       std       min       25%       50%       75%       max
A                                                                             
x    2.0  0.362296  0.371891  0.099329  0.230813  0.362296  0.493779  0.625262
y    2.0  0.473104  0.188415  0.339874  0.406489  0.473104  0.539719  0.606333
z    3.0  0.506519  1.087770 -0.607696 -0.023102  0.561492  1.063626  1.565760
Sign up to request clarification or add additional context in comments.

1 Comment

This is a great feature I knew nothing about. Thanks for sharing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.