Tan Hee
Tan Hee

Reputation: 21

How do I multiply the values from the same column based on the same index in a dataframe?

I have multiple columns and multiple distinct indexes so i am confused with the logical flow

   list1  list2  list3
A      2      3      4
A      1      5      7
B      2      3      8
B      6      1      2
C      4      2      2
C      2      4      5

my desired result would be :

   list1  list2  list3
A    2.0   15.0   28.0
B   12.0    3.0   16.0
C    8.0    8.0   10.0

Upvotes: 2

Views: 38

Answers (1)

jezrael
jezrael

Reputation: 863801

Use DataFrame.prod with level=0:

df1 = df.prod(level=0)

Alternative solution is use GroupBy.prod by index:

df1 = df.groupby(level=0).prod()
#alternative
df1 = df.groupby(df.index).prod()

print (df1)
   list1  list2  list3
A      2     15     28
B     12      3     16
C      8      8     10

Upvotes: 3

Related Questions