python – 来自两个不同数据帧的平均值

我喜欢:

material plant  Order                
24990   89952   4568789,5098710     
24990   89952   9448609,1007081     
166621  3062    18364103            
166621  3062    78309139            
240758  3062    55146035            
276009  3062    38501581,857542     

和df1像:

material plant   Order      m1     m2      m3   m4   m5
24990     89952 4568789     0.123  0.214   0.0  0.0  0.0
24990     89952 5098710     1.000  0.363   0.0  0.0  0.0
24990     89952 9448609     0.0    0.345   0.0  1.0  0.0
24990     89952 1007081     0.0    0.756   0.0  1.0  0.0
166621    3062  18364103    0.0    0.0     0.0  0.0  0.0
166621    3062  78309139    0.0    1.0     0.0  0.0  0.0
240758    3062  55146035    1.0    1.0     1.0  0.0  0.0
276009    3062  38501581    1.0    1.0     1.0  0.0  0.0
276009    3062  38575428    1.0    1.0     1.0  0.0  0.0

我想在df1中迭代Order,当df2中有Order匹配时,从m1到m5找到平均值.
我想实现像df2:

material plant  Order              avg m1 avgm2 avgm3 avgm4 avgm5
24990   89952   4568789,5098710    0.5615 0.2885 0.0   0.0   0.0
24990   89952   9448609,1007081     
166621  3062    18364103            
166621  3062    78309139            
240758  3062    55146035            
276009  3062    38501581,857542 

我正在尝试不同的方法来实现df2,例如:

df2 = (df.groupby(df1, sort=False)['Order'].apply(lambda x: ','.split(x.astype(str)))
   .mean() 
   .reset_index()
.reindex(columns=df.columns))
 print (df2)

第二:

group = df.columns[np.r_[0:3, 3:len(df.columns)]]
 res = df1.groupby(group)['Order'].apply(list).mean().reset_index()

但我不确定这是否是获得它的正确方法.

解决方法:

您可以使用:

df = (df1.join(df1.set_index(['material','plant'], append=True)['Order']
          .str.split(',', expand=True)
          .stack()
          .astype(int)
          .reset_index(name='Order')
          .merge(df2, on=['material','plant','Order'], how='left') 
          .drop(['material','plant','Order','level_3'], axis=1)
          .groupby('level_0')
          .mean())
          )
print (df)
   material  plant            Order      m1      m2   m3   m4   m5
0     24990  89952  4568789,5098710  0.5615  0.2885  0.0  0.0  0.0
1     24990  89952  9448609,1007081  0.0000  0.5505  0.0  1.0  0.0
2    166621   3062         18364103  0.0000  0.0000  0.0  0.0  0.0
3    166621   3062         78309139  0.0000  1.0000  0.0  0.0  0.0
4    240758   3062         55146035  1.0000  1.0000  1.0  0.0  0.0
5    276009   3062  38501581,857542  1.0000  1.0000  1.0  0.0  0.0

说明:

>首先按splitstack创建DataFrame
>使用merge加入第二个DataFrame并离开连接
>使用drop删除不必要的列
>然后聚合均值
>输出系列是join到第一个DataFrame

详情:

df3 = (df1.set_index(['material','plant'], append=True)['Order']
          .str.split(',', expand=True)
          .stack()
          .astype(int)
          .reset_index(name='Order')
          .merge(df2, on=['material','plant','Order'], how='left'))
print (df3)
   level_0  material  plant  level_3     Order     m1     m2   m3   m4   m5
0        0     24990  89952        0   4568789  0.123  0.214  0.0  0.0  0.0
1        0     24990  89952        1   5098710  1.000  0.363  0.0  0.0  0.0
2        1     24990  89952        0   9448609  0.000  0.345  0.0  1.0  0.0
3        1     24990  89952        1   1007081  0.000  0.756  0.0  1.0  0.0
4        2    166621   3062        0  18364103  0.000  0.000  0.0  0.0  0.0
5        3    166621   3062        0  78309139  0.000  1.000  0.0  0.0  0.0
6        4    240758   3062        0  55146035  1.000  1.000  1.0  0.0  0.0
7        5    276009   3062        0  38501581  1.000  1.000  1.0  0.0  0.0
8        5    276009   3062        1    857542    NaN    NaN  NaN  NaN  NaN

相关文章

转载:一文讲述Pandas库的数据读取、数据获取、数据拼接、数...
Pandas是一个开源的第三方Python库,从Numpy和Matplotlib的基...
整体流程登录天池在线编程环境导入pandas和xrld操作EXCEL文件...
 一、numpy小结             二、pandas2.1为...
1、时间偏移DateOffset对象DateOffset类似于时间差Timedelta...
1、pandas内置样式空值高亮highlight_null最大最小值高亮背景...