Calculating And Adding Average And Standard Deviation Columns To A Data Frame
I have: df = pd.DataFrame({'A1': [0.1,0.5,3.0, 9.0], 'A2':[2.0,4.5,1.2,9.0]}) I would like to add 2 columns to the data frame that calculate the average and standard deviation lik
Solution 1:
Let's try with assign
using mean
and std
with parameter axis=1
:
df.assign(Mean=df.mean(1), Stddev=df.std(1))
Output:
A1 A2 Mean Stddev
0 0.1 2.0 1.05 1.343503
1 0.5 4.5 2.50 2.828427
2 3.0 1.2 2.10 1.272792
3 9.0 9.0 9.00 0.000000
Edit for comment / add CpK:
df.assign(mean=df.mean(1),stddev=df.std(1)).eval('Cpk = (mean + stddev) / A2')
Output:
A1 A2 mean stddev Cpk
0 0.1 2.0 1.05 1.343503 1.196751
1 0.5 4.5 2.50 2.828427 1.184095
2 3.0 1.2 2.10 1.272792 2.810660
3 9.0 9.0 9.00 0.000000 1.000000
Post a Comment for "Calculating And Adding Average And Standard Deviation Columns To A Data Frame"