Home > other >  Apply custom functions to groupby pandas
Apply custom functions to groupby pandas

Time:08-06

I know there are questions/answers about how to use custom function for groupby in pandas but my case is slightly different.

My data is

  group_col       val_col
0         a    [1, 2, 34]
1         a        [2, 4]
2         b  [2, 3, 4, 5]

data = {'group_col': {0: 'a', 1: 'a', 2: 'b'}, 'val_col': {0: [1, 2, 34], 1: [2, 4], 2: [2, 3, 4, 5]}}
df = pd.DataFrame(data)

What I am trying to do is to group by group_col, then sum up the length of lists in val_col for each group. My desire output is

a     5
b     4

I wonder I can do this in pandas?

CodePudding user response:

You can try

df['val_col'].str.len().groupby(df['group_col']).sum()

CodePudding user response:

df.groupby('group_col')['val_col'].sum().str.len()

Output:

group_col
a    5
b    4
Name: val_col, dtype: int64
  • Related