How to use pd.concat with an un initiated dataframe?

强颜欢笑 提交于 2019-12-12 09:46:31

问题


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:


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


来源:https://stackoverflow.com/questions/43170601/how-to-use-pd-concat-with-an-un-initiated-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!