如何在熊猫数据框中替换/分配数据块

无声

我有一个称为test3看起来的数据集(从excel导入):

$ID  $spec    $orient $direct   $rep     $slope   $intercept
9119    1       stance  15b     1       2859.09223  158
9119    2       stance  15b     2       2886.53583  321
9119    3       stance  0       1       2860.91423  21
9119    4       fall    15f     1       2878.9364   326
9119    5       fall    15f     2       2902.0397   45
9120    1       stance  15b     1       1444.91347  654
9120    2       stance  15b     2       1460.09585  23
9120    3       stance  0       1       1470.2588   85
9120    4       fall    15f     1       1481.6892   225
9120    5       fall    15f     2       1475.40215  145

还有一个称为的模板数据框test3mean

$ID  $spec    $orient $direct   $slope   $intercept
9119    1       stance  15b     nan     nan
9119    2       stance  0       nan     nan
9119    3       fall    15f     nan     nan
9120    1       stance  15b     nan     nan
9120    2       stance  0       nan     nan
9120    3       fall    15f     nan     nan

我使用熊猫数据框计算平均值的$slope,并$intercept在反复测量test3,并把它们在相应的栏目test3mean

ID = np.array([9119,9120])
orient = np.array(['stance','fall'])
direct = np.array(['0','15f','15b'])

for i in ID:
    for o in orient:
        for d in direct:
            test3mean[test3mean['$ID']==i][test3mean['$orient']==o][test3mean['$direct']==d][['$slope','$intercept']] = test3[test3['$ID']==i][test3['$orient']==o][test3['$direct']==d][test3['$rep']!=3].mean()[['$slope','$intercept']]

但是,test3mean不会改变。我知道复制与查看问题,并且已经查看了df.loc[:,()]解决方案,但是无法针对我的具体情况实施它们。

循环两步后的预期输出将是test3mean的样子:

$ID  $spec    $orient $direct   $slope       $intercept
9119    1       stance  15b     2872.81403  239.5
9119    2       stance  0       2860.91423  21
9119    3       fall    15f     nan     nan
9120    1       stance  15b     nan     nan
9120    2       stance  0       nan     nan
9120    3       fall    15f     nan     nan
安塞夫

用途DataFrame.groupby

result = (test3[test3['$rep'].ne(3)].groupby(['$ID','$orient','$direct'],as_index=False)['$slope','$intercept']
                                    .mean())

#    $ID $orient $direct       $slope  $intercept
#0  9119    fall     15f  2890.488050       185.5
#1  9119  stance       0  2860.914230        21.0
#2  9119  stance     15b  2872.814030       239.5
#3  9120    fall     15f  1478.545675       185.0
#4  9120  stance       0  1470.258800        85.0
#5  9120  stance     15b  1452.504660       338.5

如果您需要合并test3mean使用DataFrame.mergehow ='left'

(test3mean.drop(['$slope','$intercept'],axis=1)
          .merge(result,on=['$ID','$orient','$direct'],how='left'))

输出量

    $ID  $spec $orient $direct       $slope  $intercept
0  9119      1  stance     15b  2872.814030       239.5
1  9119      2  stance       0  2860.914230        21.0
2  9119      3    fall     15f  2890.488050       185.5
3  9120      1  stance     15b  1452.504660       338.5
4  9120      2  stance       0  1470.258800        85.0
5  9120      3    fall     15f  1478.545675       185.0

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章