[英]How to divide a pandas pivot table by a dataframe with a difference shape?
Objective: I have a pivot table, where I would like to divide each cell by a value from my dataframe, if there is a match.目标:我有一个 pivot 表,如果匹配,我想将每个单元格除以 dataframe 中的值。
Specifically, all the cells in the column 0 should be divided by 4 because Store1 is 4 in the dataframe.具体来说,第 0 列中的所有单元格应除以 4,因为 dataframe 中的 Store1 为 4。 Similarly, the last column would be divided by 3.
同样,最后一列将除以 3。
The expected outcome is...预期的结果是...
Data:数据:
df = pd.DataFrame({'Start':['Store1','Store1','Store1','Store2','Store2','Store2','Store3','Store3','Store3'],
'Stop':['Store1','Store2','Store3','Store1','Store2','Store3','Store1','Store2','Store3'],
'Distance':[0,100,200,100,0,100,100,100,0]}).pivot(columns='Start', index = 'Stop', values=None)
df_div = pd.DataFrame({'Distance':['Store1','Store3'],'Import':[4,3]})
df_div = df_div.set_index('Distance')
Here's a solution with a for loop:这是一个带有for循环的解决方案:
for store in df_div.index:
divider = df_div.loc[store,][0]
df.loc[:,(slice(None),store)] = df.loc[:,(slice(None),store)]/divider
Output of df : Output 的df :
Distance
Start Store1 Store2 Store3
Stop
Store1 0.0 100 33.333333
Store2 25.0 0 33.333333
Store3 50.0 100 0.000000
If you want to include only stores from df_div in df , you can use this outside the for loop:如果您只想在df中包含来自df_div的商店,您可以在 for 循环之外使用它:
df = df.loc[:,(slice(None),list(df_div.index))]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.