问题
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 describe
accomplishes 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