简体   繁体   English

计算逐行平均熊猫python

[英]Calculate row-wise average pandas python

I am new to python.我是python的新手。 I want to calculate the row-wise average based on unique IDs.我想根据唯一 ID 计算逐行平均值。

My DataFrame is:我的数据帧是:

       ID            Time[h]  concentration[g/L]  
15127  V527          23.425      59.9  
20361  V527          27.570      73.4  
21880  V527          29.281      75.4
33133  V560          27.677      75.9  
35077  V560          30.183      75.7  
37117  V560          31.847      74.6  

I want to calculate the row wise average based on each ID.我想根据每个 ID 计算行平均。 so that my output looks something like this所以我的输出看起来像这样

       ID            Time[h]  concentration[g/L]  avg [g/L]
15127  V527          23.425      59.9               NaN
20361  V527          27.570      73.4               66.5
21880  V527          29.281      75.4               74.4
33133  V560          27.677      75.9               NaN 
35077  V560          30.183      75.7               66.5 
37117  V560          31.847      74.6               75.8 

I tried:我试过:

df.groupby(['ID'])['concentration[g/L]'].mean()

But this returned mean for each ID, as a whole.但这返回了每个 ID 的平均值,作为一个整体。

So I tried this:所以我试过这个:

df.groupby(['ID'])['concentration[g/L]'].transform('mean')

This returns again the mean of each group, but fills to the same length of my df.这再次返回每个组的平均值,但填充到我的 df 相同的长度。

Can you please help me, if something is not clear I can rephrase my question.你能帮我吗,如果有什么不清楚的,我可以改写我的问题。

Thanks in Advance!提前致谢!

Try using pd.rolling.mean with a window of 2:尝试使用窗口为 2 的pd.rolling.mean

>>> df['avg [g/L]'] = df.groupby('ID')['concentration[g/L]'].rolling(2).mean().values
>>> df
         ID  Time[h]  concentration[g/L]  avg [g/L]
15127  V527   23.425                59.9        NaN
20361  V527   27.570                73.4      66.65
21880  V527   29.281                75.4      74.40
33133  V560   27.677                75.9        NaN
35077  V560   30.183                75.7      75.80
37117  V560   31.847                74.6      75.15

You can use shift :您可以使用shift

df['avg'] = df.groupby('ID')['concentration[g/L]'].apply(lambda x: (x + x.shift())/2)

print(df)

         ID  Time[h]  concentration[g/L]    avg
15127  V527   23.425                59.9    NaN
20361  V527   27.570                73.4  66.65
21880  V527   29.281                75.4  74.40
33133  V560   27.677                75.9    NaN
35077  V560   30.183                75.7  75.80
37117  V560   31.847                74.6  75.15

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM