Sean M.
Sean M.

Reputation: 593

Python2.7 - Pandas dataframe groupby two criterias

Lets say I have a panadas DataFrame:

import pandas as pd

df = pd.DataFrame(columns=['name','time'])
df = df.append({'name':'Waren', 'time': '20:15'}, ignore_index=True)
df = df.append({'name':'Waren', 'time': '20:12'}, ignore_index=True)
df = df.append({'name':'Waren', 'time': '20:11'}, ignore_index=True)
df = df.append({'name':'Waren', 'time': '01:29'}, ignore_index=True)
df = df.append({'name':'Waren', 'time': '02:15'}, ignore_index=True)
df = df.append({'name':'Waren', 'time': '02:16'}, ignore_index=True)

df = df.append({'name':'Kim', 'time': '20:11'}, ignore_index=True)
df = df.append({'name':'Kim', 'time': '01:29'}, ignore_index=True)
df = df.append({'name':'Kim', 'time': '02:15'}, ignore_index=True)
df = df.append({'name':'Kim', 'time': '01:49'}, ignore_index=True)
df = df.append({'name':'Kim', 'time': '01:49'}, ignore_index=True)
df = df.append({'name':'Kim', 'time': '02:15'}, ignore_index=True)
df = df.append({'name':'Mary', 'time': '22:15'}, ignore_index=True)
df = df.drop(df.index[2])
df = df.drop(df.index[7])

I would like to group this frame by name and secondly group by continuous indexes (Group by continuous indexes in Pandas DataFrame).

The desired output would be a grouping like this:

Desired Output

So the rows are grouped by name and for row this continuous increasing indexes only the first and last element is taken.

I tried it like so: df.groupby(['name']).groupby(df.index.to_series().diff().ne(1).cumsum()).group which only raises the error: AttributeError: Cannot access callable attribute 'groupby' of 'DataFrameGroupBy' objects, try using the 'apply' method

Any help is welcome!

Upvotes: 0

Views: 143

Answers (1)

Umesh
Umesh

Reputation: 971

You are doing it wrong. When you do df.groupby(['name']) it returns attribute groupby which is not callable. You need to apply both of it together.


df.groupby(['name', df.index.to_series().diff().ne(1).cumsum()]).groups

Out: 
{('Kim', 2): [6, 7],
 ('Kim', 3): [9, 10, 11],
 ('Mary', 3): [12],
 ('Waren', 1): [0, 1],
 ('Waren', 2): [3, 4, 5]}

Upvotes: 1

Related Questions