我想实现的目的是创建一个新的列,称为均值,它使我得到value1的每日平均值,然后将value2除以该每日平均值,并将其存储为value3。
这是我所拥有的
df = pd.DataFrame( [
[pd.Timestamp('2019-09-22 00:00:00'), 'device1', 10, 3000],
[pd.Timestamp('2019-09-22 04:00:00'), 'device1', 40, 2000],
[pd.Timestamp('2019-09-22 05:00:00'), 'device1', 45, 1000],
[pd.Timestamp('2019-09-22 06:00:00'), 'device1', 450, 1500],
[pd.Timestamp('2019-09-22 07:00:00'), 'device1', 500, 2000],
[pd.Timestamp('2019-09-22 08:00:00'), 'device1', 550, 3000],
[pd.Timestamp('2019-09-22 15:00:00'), 'device1', 600, 4000],
[pd.Timestamp('2019-09-22 16:00:00'), 'device1', 650, 3000],
[pd.Timestamp('2019-09-22 17:00:00'), 'device1', 700, 2000],
[pd.Timestamp('2019-09-22 21:00:00'), 'device1', 900, 1000],
[pd.Timestamp('2019-09-22 22:00:00'), 'device1', 1000, 1500],
[pd.Timestamp('2019-09-23 05:00:00'), 'device1', 1100, 2000],
[pd.Timestamp('2019-09-23 04:00:00'), 'device1', 1200, 3000],
[pd.Timestamp('2019-09-24 05:00:00'), 'device1', 1100, 2000],
[pd.Timestamp('2019-09-24 04:00:00'), 'device1', 1200, 3000]
],
columns=["devicetimestamp","id","value", "value2"]
)
我想要达到的目标是这样的>>
devicetimestamp , id , value1 , value2, mean, value3 [pd.Timestamp('2019-09-22 00:00:00'), 'device1', 10, 3000, 404.09, 0.134], [pd.Timestamp('2019-09-22 04:00:00'), 'device1', 40, 2000, 404.09, 0.202], [pd.Timestamp('2019-09-22 05:00:00'), 'device1', 45, 1000, 404.09, 0.404], [pd.Timestamp('2019-09-22 06:00:00'), 'device1', 450, 1500, 404.09, 0.269], [pd.Timestamp('2019-09-22 07:00:00'), 'device1', 500, 2000, 404.09], [pd.Timestamp('2019-09-22 08:00:00'), 'device1', 550, 3000, 404.09], [pd.Timestamp('2019-09-22 15:00:00'), 'device1', 600, 4000, 404.09], [pd.Timestamp('2019-09-22 16:00:00'), 'device1', 650, 3000, 404.09], [pd.Timestamp('2019-09-22 17:00:00'), 'device1', 700, 2000, 404.09], [pd.Timestamp('2019-09-22 21:00:00'), 'device1', 900, 1000, 404.09], [pd.Timestamp('2019-09-22 22:00:00'), 'device1', 1000, 1500, 404.09], [pd.Timestamp('2019-09-23 05:00:00'), 'device1', 1100, 2000, 1150, 1.04], [pd.Timestamp('2019-09-23 04:00:00'), 'device1', 1200, 3000, 1150, 0.95], [pd.Timestamp('2019-09-24 05:00:00'), 'device1', 1100, 2000, 1200, 1.09], [pd.Timestamp('2019-09-24 04:00:00'), 'device1', 1300, 3000, 1200, 0.92]
'''
我试图做一个groupby.mean并幻化新列,但这没用
我想实现的目的是创建一个新的列,称为均值,它使我得到value1的每日平均值,然后将value2除以该每日平均值,并将其存储为value3。这就是我所拥有的...
您要transform('mean')
: