快速复杂的熊猫和字典操作



我是熊猫的新手,请就棘手的数据透视表操作寻求一些建议。

我有两个熊猫数据透视表和一本字典。第一个数据透视表有一些值为零。第二个数据透视表具有相同的因子和水平,但值不同。字典是每个因子的所有可能水平对的集合。示例代码:

df = pd.DataFrame({'MyColumn1': ['A', 'A', 'B', 'B'],
'MyColumn2': ['M', 'N', 'M', 'P'],
'Value': [1, 1, 1, 1]})
table = pd.pivot_table(df, values='Value', index=['MyColumn1', 'MyColumn2'], aggfunc=np.sum, fill_value = 0, dropna = False)
df2 = pd.DataFrame({'MyColumn1': ['A', 'A', 'A', 'B', 'B', 'B'],
'MyColumn2': ['M', 'N', 'P', 'M', 'N', 'P'],
'Value': [5, 10, 15, 20, 25, 30]})
table2 = pd.pivot_table(df2, values='Value', index=['MyColumn1', 'MyColumn2'], aggfunc=np.sum)
myDictionary = {'MyColumn1': {('A', 'A'): 10, ('A', 'B'): 20, 
('B', 'A'): 30, ('B', 'B'): 40},
'MyColumn2': {('M', 'M'): 1, ('M', 'N'): 2, ('M', 'P'): 3,
('N', 'M'): 4, ('N', 'N'): 5, ('N', 'P'): 6,
('P', 'M'): 7, ('P', 'N'): 8, ('P', 'P'): 9}}

此代码生成下表:

TABLE                Value
MyColumn1 MyColumn2       
A         M              1
N              1
P              0
B         M              1
N              0
P              1
TABLE2               Value
MyColumn1 MyColumn2       
A         M              5
N             10
P             15
B         M             20
N             25
P             30

对于Table中的每个非零条目,我想 (1( 遍历Table中的每个零条目,并在 myDictionary 中找到对应于零输入键和非零输入键的数字的乘积,(2( 将每个乘积乘以Table2中的相应值作为零行, 然后(3(找到这些产品的总和。

例如,使用上述数据,算法将计算:

(A, P) -> (A, M) = 10 * 7 = 70, 70 * 15 = 1050
(A, P) -> (A, N) = 10 * 8 = 80, 80 * 15 = 1200
(A, P) -> (B, M) = 20 * 7 = 140, 140 * 15 = 2100
(A, P) -> (B, P) = 20 * 9 = 180, 180 * 15 = 2700
(B, N) -> (A, M) = 30 * 4 = 120, 120 * 25 = 3000
(B, N) -> (A, N) = 30 * 5 = 150, 150 * 25 = 3750
(B, N) -> (B, M) = 40 * 4 = 160, 160 * 25 = 4000
(B, N) -> (B, P) = 40 * 6 = 240, 240 * 25 = 6000

前四行对应于第一个零条目,后四行对应于Table中的第二个零条目。有四行,因为有四个非零条目。接下来,算法乘以Table2中的相应值。最后,它将为每个非零条目加起来:

(A, M) total = 1050 + 3000 = 4050
(A, N) total = 1200 + 3750 = 4950
(B, M) total = 2100 + 4000 = 6100
(B, P) total = 2700 + 6000 = 8700

所需的结果是形状与原始表相同但以这些总和作为值的数据透视表。

Value
MyColumn1 MyColumn2       
A         M           4050
N           4950
P              0
B         M           6100
N              0
P           8700

我正在寻找一种有效地执行此操作的方法,因为在实践中,我将有 100,000+ 个零条目、<1000 个非零条目和 ~10 列。出于性能原因,我不确定如何构建循环,或者即使循环是执行此操作的最有效方法。任何帮助将不胜感激,谢谢:)

不确定这对您的实际数据有多快,但这就是我要做的:

col1_df = pd.Series(myDictionary['MyColumn1']).unstack()
col2_df = pd.Series(myDictionary['MyColumn2']).unstack()
out_df = pd.DataFrame()
# loop through columns
for col in table.columns:    
zeros = table['Value'].eq(0)
non_zero_idx = np.array(table.index[~zeros].to_list())
zero_idx = np.array(table.index[zeros].to_list())
num_nz, num_z = len(non_zero_idx), len(zero_idx)
xs,ys = np.meshgrid(np.arange(num_z),np.arange(num_nz))
xs, ys = xs.ravel(), ys.ravel()
col1 = col1_df.lookup(zero_idx[xs,0], non_zero_idx[ys,0])
col2 = col2_df.lookup(zero_idx[xs,1], non_zero_idx[ys,1])
prods = (col1* col2).reshape(num_nz, num_z).T
values = table2.loc[zeros, ['Value']].values
out_df[col] = (pd.Series((prods * values).sum(0), index=non_zero_idx)
.reindex(table.index, fill_value=0)
)

输出:

Value
MyColumn1 MyColumn2       
A         M           4050
N           4950
P              0
B         M           6100
N              0
P           8700

最新更新