How to use pd.concat with un init-dataframe?

I want to be able to concatenate the data results into memory as they pass through the function and end up with a completely new dataframe with the results. How can I do this without creating a complete dataframe in front of 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 flesh out the results myFuncOnDF()

into a new dataframe that doesn't exist yet?

+3


source to share


1 answer


Not sure what you expected, but groupby

it describe

does the same



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

      

+5


source







All Articles