Charlotte Deng
Charlotte Deng

Reputation: 131

Perform calculation for different group python pandas

I have a pandas data set with different groups. For each group, I want to apply different calculations. What's the best way to do it?

For example,

Input data

Group val1 val2
1      12   3
1      19   4
2      23   9
3      59   74
3      2    44

Output data

Group val1 val2 output
1      12   3   3*val1*val2
1      19   4   3*val1*val2
2      23   9   5*val1*val2
3      59   74  10*val1*val2
3      2    44  10*val1*val2

Upvotes: 0

Views: 123

Answers (3)

David Erickson
David Erickson

Reputation: 16673

If that string is the literal output you are seeking, then you can create a dictionary for each group and map the values. Then just add the string to the end:

dct = {1:3, 2:5, 3:10}
df['output'] = df['Group'].map(dct).astype(str) + '*val1*val2'
df
Out[1]: 
   Group  val1  val2        output
0      1    12     3   3*val1*val2
1      1    19     4   3*val1*val2
2      2    23     9   5*val1*val2
3      3    59    74  10*val1*val2
4      3     2    44  10*val1*val2

Now, I took your output verbatim, but if you are tyring to multiply these values :) , then you can achieve this like:

dct = {1:3, 2:5, 3:10}
df['output'] = df['Group'].map(dct) * df['val1'] * df['val2']
df
Out[1]: 
   Group  val1  val2  output
0      1    12     3     108
1      1    19     4     228
2      2    23     9    1035
3      3    59    74   43660

Upvotes: 1

Andy L.
Andy L.

Reputation: 25239

You need to create a mapping/dictionary d to link groups to their calculation values. Next, map it to columns Group and use numpy ufunc `reduce of multiply to create desired output

import numpy as np

d = {1: 3, 2: 5, 3: 10}
df['output'] = np.multiply.reduce([df.Group.map(d), df.val1, df.val2])

Out[243]:
   Group  val1  val2  output
0      1    12     3     108
1      1    19     4     228
2      2    23     9    1035
3      3    59    74   43660
4      3     2    44     880

If you don't want to use numpy, just do multiplication each column

df['output'] = df.Group.map(d) * df.val1 * df.val2

Upvotes: 1

Morteza Akbari
Morteza Akbari

Reputation: 34

I guess you could use apply

df.groupby([“group”, “val1”, “val2”]).apply(lambda x: x)

Upvotes: -1

Related Questions