python - Groupby an numpy.array based on groupby of a pandas.DataFrame with the same length -
i have numpy.array arr
, pandas.dataframe df
.
arr
, df
have same shape (x,y)
.
i need group 1 column of df
, apply transformation of impacted rows on arr
have same shape.
to clear, here toy example:
arr = 0 1 12 3 2 5 45 47 3 19 11 111 df = b c d 0 0 1 2 3 1 4 5 6 7 2 4 9 10 11
i want group df
a
, compute mean in place of transforming df
want arr
transformed.
so like:
arr = 0 1 12 3 (2+3)/2 (5+19)/2 (45+11)/2 (47+111)/2
is possible? no expensive loops?
thanks in advance
it looks need first create dataframe
arr
, groupby
column a
, aggregate mean
. last convert numpy array
values
:
print (pd.dataframe(arr).groupby(df.a).mean().values) [[ 0. 1. 12. 3. ] [ 2.5 12. 28. 79. ]]
Comments
Post a Comment