I've aggregated some data and i'd like to get the aggregation values back into another dataframe. I've aggregated the data like this
bycluster_type = df.groupby(['cluster', 'Type'])
tCount = bycluster_type['Type'].agg([len])
Edit: From this point i made some mistakes which i've corrected now and added a few new thoughts.
tCount is now a DataFrame with a MultiIndex. What i'd lke to do now is to get the cluster, type and the corresponding value to put it together with some other data in another dataframe. For example:
>>> tCount
len
cluster Type
1.0 M 1
2.0 M 7
4.0 M 2
So the next step is to get the index and the row:
index, row = next(tCount.index.values)
So what i'd do next is to unzip the index tuple into cluster and type and get the len value from the row.
cluster, type = index
val = row['len']
Is there a more efficent or elegant way of achieving my goal?
Edit: Some example data
cluster, Type, foo
1, M, f
1, T, o
1, S, o
2, M, f
2, M, o
3, T, o
tCount.to_frame('len').reset_index()combine_firstorconcatthat you are after. In case the index of the other dataframe is similar thendf2.loc[tCount.index] = tCount.valuesmight help. We need more clear info for a good answer.