0

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
6
  • Can you add some data samples? Commented Apr 10, 2018 at 14:10
  • 1
    tCount.to_frame('len').reset_index() Commented Apr 10, 2018 at 14:12
  • 2
    The degree of elegance depends on what you want to do and you haven’t given enough information. There are many things we could do. We don’t know which thing to show you. Commented Apr 10, 2018 at 14:25
  • Perhaps its combine_first or concat that you are after. In case the index of the other dataframe is similar then df2.loc[tCount.index] = tCount.values might help. We need more clear info for a good answer. Commented Apr 10, 2018 at 14:26
  • Hey i've added some sample data. i hope this helps. Commented Apr 10, 2018 at 14:56

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You are right i've just edited it because i noticed i did something wrong. And thank you very much for the reset_index() that's exactly what i was looking for.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.