Reputation: 79
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
Upvotes: 0
Views: 68
Reputation: 801
Ran your code and found that tCount results in a MultiIndex Dataframe.
You don't need to iterate throug the index, df = tCount.reset_index()
should do the trick.
Upvotes: 1