我想计算熊猫数据框中“值”的标准偏差,条件是为常见的“ groupped_measurement”计算得出。计算之后,我想计算注释行。
我尝试过以下行:
df['standard_deviation'] = df['groupped_measurement'].diff().fillna(df['value']).std()
,但无法正常工作。我的完整代码如下所示:
import pandas as pd
import numpy as np
# Define input dataframe
df = {'servo_in_position': [1,1,1,0,0,0,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1],
'value': [0.2,2.1,3.5,6.7,2.1,3.4,5.7,9.6,3.2,1.2,6.3,8.5,7.4,6.2,3.4,3.8,1.7,2.8,7.6,4.5,9.0]}
df = pd.DataFrame(df,columns= ['servo_in_position','value'])
print("Dataframe is:\n",df)
print("Groupping data according to servo positions, please wait...")
df['groupped_measurement'] = df['servo_in_position'].diff().fillna(df['servo_in_position']).eq(1).cumsum().mask(df['servo_in_position'] == 0, 0)
df['standard_deviation'] = df['groupped_measurement'].diff().fillna(df['value']).std()
# df=df.groupby('groupped_measurement',as_index=False).mean()
# df['new_value']=df['standard_deviation']*100/df['value']
print("Data groupped successfully!")
print("Input data:\n",df)
预期的输出如下:
servo_in_position value groupped_measurement standard_deviation
0 1 0.2 1 1.6563011
1 1 2.1 1 1.6563011
2 1 3.5 1 1.6563011
3 0 6.7 0 0
4 0 2.1 0 0
5 0 3.4 0 0
6 1 5.7 2 3.194526569
7 1 9.6 2 3.194526569
8 1 3.2 2 3.194526569
9 1 1.2 2 3.194526569
10 1 6.3 2 3.194526569
11 0 8.5 0 0
12 0 7.4 0 0
13 0 6.2 0 0
14 0 3.4 0 0
15 1 3.8 3 2.832666588
16 1 1.7 3 2.832666588
17 1 2.8 3 2.832666588
18 1 7.6 3 2.832666588
19 1 4.5 3 2.832666588
20 1 9 3 2.832666588
答案 0 :(得分:1)
您可以简化代码-创建Series
s1
和s2
,然后将std
与GroupBy.transform
一起使用,以用聚集值填充新列,根据条件为集合0
添加了numpy.where
:
mask = df['servo_in_position'] == 0
s1 = df['servo_in_position'].diff().ne(0).cumsum()
s2 = df['value'].groupby(s1).transform('std')
#if need omit helper column simple remove df['groupped_measurement'] = np.where(mask, 0, s1)
df['groupped_measurement'] = np.where(mask, 0, s1)
df['standard_deviation'] = np.where(mask, 0, s2)
print("Dataframe is:\n",df)
servo_in_position value groupped_measurement standard_deviation
0 1 0.2 1 1.656301
1 1 2.1 1 1.656301
2 1 3.5 1 1.656301
3 0 6.7 0 0.000000
4 0 2.1 0 0.000000
5 0 3.4 0 0.000000
6 1 5.7 3 3.194527
7 1 9.6 3 3.194527
8 1 3.2 3 3.194527
9 1 1.2 3 3.194527
10 1 6.3 3 3.194527
11 0 8.5 0 0.000000
12 0 7.4 0 0.000000
13 0 6.2 0 0.000000
14 0 3.4 0 0.000000
15 1 3.8 5 2.832667
16 1 1.7 5 2.832667
17 1 2.8 5 2.832667
18 1 7.6 5 2.832667
19 1 4.5 5 2.832667
20 1 9.0 5 2.832667
答案 1 :(得分:1)
首先,我们创建一个系列s
,它将servo_in_pisition
的每个更改定义为一个唯一的组。
然后我们在这些组上GroupBy.transform(std)
。我们使用transform
返回等长向量,因此可以将其定义为现有数据帧的新列,否则数据将被汇总。
如果np.where
的值,我们使用std
有条件地分配servo_in_pisition != 0
s1 = df['servo_in_position'].diff().ne(0).cumsum()
s2 = df.groupby(s)['value'].transform('std')
df['standard_deviation'] = np.where(df['servo_in_position'].ne(0), s2, 0)
输出
servo_in_position value standard_deviation
0 1 0.2 1.656301
1 1 2.1 1.656301
2 1 3.5 1.656301
3 0 6.7 0.000000
4 0 2.1 0.000000
5 0 3.4 0.000000
6 1 5.7 3.194527
7 1 9.6 3.194527
8 1 3.2 3.194527
9 1 1.2 3.194527
10 1 6.3 3.194527
11 0 8.5 0.000000
12 0 7.4 0.000000
13 0 6.2 0.000000
14 0 3.4 0.000000
15 1 3.8 2.832667
16 1 1.7 2.832667
17 1 2.8 2.832667
18 1 7.6 2.832667
19 1 4.5 2.832667
20 1 9.0 2.832667