[英]Multiply Python Pandas dataframes together to get product of values in column
我需要創建Python函數來實現以下幫助:
1)以3個Pandas數據幀作為輸入(在第二列中包含一個索引列,以及一個關聯的整數或浮點值)。 這些定義如下:
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)使用這三個數據框,創建一個新的數據框('df4'),該數據框在前3列中組織3個索引的所有可能組合;
3)在第4列中,附加來自三個源數據幀的所有關聯“值”的數學乘積。 因此,該函數的DataFrame輸出應類似於: https ://ibb.co/cypEY6
在此先感謝您的幫助。
科林
使用所有索引和列的product
,並通過構造函數創建DataFrame
,對於所有所有列,請使用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
另一種方法是multiple
通過列表理解所有的值:
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
帶有DataFrames
列表的動態解決方案,每個中都必須有相同的columnname value
:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.