简体   繁体   中英

How to groupby pandas DataFrame by customized function

have a dataframe of the form

col1   sum
801    1
802    2
391    3
701    5

I want to groupby the initial number of col1, applying mean

basically result should be

col1    sum
8       1.5
3       3
7       5

what I have tried is

def group_condition(col1):
    col1 = str(col1)
    if col1.startswith('8'):
        return 'y'
    else:
        return 'n'


augmented_error_table[[sum]].groupby(augmented_error_table[col1].groupby(group_condition).groups).mean()

But it doesn't work out, give me empty df

Use astype(str) in groupby like .

df.groupby(df['col1'].astype(str).str[0])['sum'].mean()

Ouptut :

      sum
col1     
3     3.0
7     5.0
8     1.5

我认为问题在于, groupby实际上需要一个序列,而不是一个函数作为输入,像这样

table.groupby(group_condition(table[col1]))
import pandas as pd
import numpy as np

df = pd.DataFrame(dict(col1=[801,802,391,701], sum=[1,2,3,5]))
# work out initial digit by list comprehension
df['init_digit'] = [str(x)[0] for x in df.col1]
# use groupby, agg function apply to sum column only
df.groupby(['init_digit']).agg({'sum':mean})

Out[23]: 
            sum
init_digit     
3           3.0
7           5.0
8           1.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