简体   繁体   中英

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:

期望的输出

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!

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]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM