Python 获取新数据框列中行值和行平均值之间的最大相对差

Python 获取新数据框列中行值和行平均值之间的最大相对差,python,pandas,mean,difference,Python,Pandas,Mean,Difference,我希望有一个额外的列,其中包含行值的最大相对差[-]和这些行的平均值: df中充满了数年的能源使用数据。 我得到的理论公式如下: df['max_rel_dif']=max[abs最高能耗–平均能耗,abs最低能耗–平均能耗]/平均能耗 初始数据帧: 所需数据帧: 试用代码: import pandas as pd
 import numpy as np

 df = pd.DataFrame({"ID": [23,43,36,87,90,98],
 "y_

我希望有一个额外的列,其中包含行值的最大相对差[-]和这些行的平均值:

df中充满了数年的能源使用数据。 我得到的理论公式如下: df['max_rel_dif']=max[abs最高能耗–平均能耗,abs最低能耗–平均能耗]/平均能耗

初始数据帧:

所需数据帧:

试用代码:

import pandas as pd

import numpy as np



df = pd.DataFrame({"ID": [23,43,36,87,90,98],
   
               "y_2010": [22631,27456,61200,87621,58951,24587],
   
               "y_2011": [21954,29654,np.nan,86542,57486,25478],
    
               "y_2012": [22314,28159,np.nan,87542,2000,np.nan],
     
               "y_2013": [22032,28654,31895,88456,0,24896,],
           
               "y_2014": [21843,2000,1600,86961,0,25461]})



print(df)



a = df.loc[:, ['y_2010','y_2011','y_2012','y_2013', 'y_2014']]



# calculate mean

mean = a.mean(1)


# calculate max_rel_dif
df['max_rel_dif'] = (((df.max(axis=1).sub(mean)).abs(),(df.min(axis=1).sub(mean)).abs()).max()).div(mean)


    # AttributeError: 'tuple' object has no attribute 'max'
-> I'm obviously doing the wrong thing with the tuple, I just don't know how to get the maximum values
   from the tuples and divide them then by the mean in the proper Phytonic way



我觉得整个功能可以

s=df.filter(like='y')
s.sub(s.mean(1),axis=0).abs().max(1)/s.mean(1)
0    0.021494
1    0.913736
2    0.949311
3    0.011800
4    1.488707
5    0.020653
dtype: float64
import pandas as pd

import numpy as np



df = pd.DataFrame({"ID": [23,43,36,87,90,98],
   
               "y_2010": [22631,27456,61200,87621,58951,24587],
   
               "y_2011": [21954,29654,np.nan,86542,57486,25478],
    
               "y_2012": [22314,28159,np.nan,87542,2000,np.nan],
     
               "y_2013": [22032,28654,31895,88456,0,24896,],
           
               "y_2014": [21843,2000,1600,86961,0,25461]})



print(df)



a = df.loc[:, ['y_2010','y_2011','y_2012','y_2013', 'y_2014']]



# calculate mean

mean = a.mean(1)


# calculate max_rel_dif
df['max_rel_dif'] = (((df.max(axis=1).sub(mean)).abs(),(df.min(axis=1).sub(mean)).abs()).max()).div(mean)


    # AttributeError: 'tuple' object has no attribute 'max'
-> I'm obviously doing the wrong thing with the tuple, I just don't know how to get the maximum values
   from the tuples and divide them then by the mean in the proper Phytonic way


s=df.filter(like='y')
s.sub(s.mean(1),axis=0).abs().max(1)/s.mean(1)
0    0.021494
1    0.913736
2    0.949311
3    0.011800
4    1.488707
5    0.020653
dtype: float64