我需要帮助创建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
非常感谢您的帮助。
科林
答案 0 :(得分:2)
使用所有索引和列的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
列表的动态解决方案,每个列表中只需要相同的列名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