简体   繁体   中英

Multiply Python Pandas dataframes together to get product of values in column

I need help creating a Python function to achieve the following:

1) Take 3 Pandas dataframes as input (containing an index column, and an associated integer or float value in the second column). These are defined as follows:

import pandas as pd

df1=pd.DataFrame([['placementA',2],['placementB',4]],columns=
['placement','value'])
df1.set_index('placement',inplace=True)

df2=pd.DataFrame([['strategyA',1],['strategyB',5],['strategyC',6]],columns=
['strategy','value'])
df2.set_index('strategy',inplace=True)

df3=pd.DataFrame([['categoryA',1.5],['categoryB',2.5]],columns=
['category','value'])
df3.set_index('category',inplace=True)

2) Using these three dataframes, create a new dataframe ('df4') which organises all possible combinations of the 3 indices across the first 3 columns;

3) In the 4th column, append the mathematical product of all the associated 'values' from the three source dataframes. The DataFrame output of the function should therefore look like: https://ibb.co/cypEY6

Many thanks in advance for your help.

Colin

Use product of all indexes and columns and create DataFrame by constructor, for multiple all columns use prod :

from  itertools import product

names = ['placement','strategy','category']
mux = pd.MultiIndex.from_product([df1.index, df2.index, df3.index], names=names)
df = (pd.DataFrame(list(product(df1['value'], df2['value'], df3['value'])), index=mux)
       .prod(1).reset_index(name='mult'))
print (df)
     placement   strategy   category  mult
0   placementA  strategyA  categoryA   3.0
1   placementA  strategyA  categoryB   5.0
2   placementA  strategyB  categoryA  15.0
3   placementA  strategyB  categoryB  25.0
4   placementA  strategyC  categoryA  18.0
5   placementA  strategyC  categoryB  30.0
6   placementB  strategyA  categoryA   6.0
7   placementB  strategyA  categoryB  10.0
8   placementB  strategyB  categoryA  30.0
9   placementB  strategyB  categoryB  50.0
10  placementB  strategyC  categoryA  36.0
11  placementB  strategyC  categoryB  60.0

Alternative is multiple all values by list comprehension:

import operator
import functools
from  itertools import product

names = ['placement','strategy','category']
a = list(product(df1.index, df2.index, df3.index))

b = product(df1['value'], df2['value'], df3['value'])
data = [functools.reduce(operator.mul, x, 1) for x in b]

df = pd.DataFrame(a, columns=names).assign(mult=data)
print (df)
     placement   strategy   category  mult
0   placementA  strategyA  categoryA   3.0
1   placementA  strategyA  categoryB   5.0
2   placementA  strategyB  categoryA  15.0
3   placementA  strategyB  categoryB  25.0
4   placementA  strategyC  categoryA  18.0
5   placementA  strategyC  categoryB  30.0
6   placementB  strategyA  categoryA   6.0
7   placementB  strategyA  categoryB  10.0
8   placementB  strategyB  categoryA  30.0
9   placementB  strategyB  categoryB  50.0
10  placementB  strategyC  categoryA  36.0
11  placementB  strategyC  categoryB  60.0

Dynamic solution with list of DataFrames , only is necessary same columnname value in each one:

dfs = [df1, df2, df3]

names = ['placement','strategy','category']
a = list(product(*[x.index for x in dfs]))
b = list(product(*[x['value'] for x in dfs]))
data = pd.DataFrame(b).product(1)

df = pd.DataFrame(a, columns=names).assign(mult=data)
print (df)
     placement   strategy   category  mult
0   placementA  strategyA  categoryA   3.0
1   placementA  strategyA  categoryB   5.0
2   placementA  strategyB  categoryA  15.0
3   placementA  strategyB  categoryB  25.0
4   placementA  strategyC  categoryA  18.0
5   placementA  strategyC  categoryB  30.0
6   placementB  strategyA  categoryA   6.0
7   placementB  strategyA  categoryB  10.0
8   placementB  strategyB  categoryA  30.0
9   placementB  strategyB  categoryB  50.0
10  placementB  strategyC  categoryA  36.0
11  placementB  strategyC  categoryB  60.0

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