Home > Enterprise >  Pandas standard deviation of column values
Pandas standard deviation of column values

Time:06-15

I have 4 columns that I want the standard deviation of in a dataframe.

Key  A   B   C   D   stdDev
X    1   2   3   4   std(1,2,3,4)
y    4   5   6   7   std(4,5,6,7)
z    8   9   10  11  std(8,9,10,11)

I want to take the values from ABCD and use them to find the stdDev of. I need this to be done for each row.

Thanks for any help

CodePudding user response:

I think you should be able to figure out how to do this on your own with the public documentation. Anyhow:

import pandas as pd 

my_dict = {
    "key": ["x", "y", "z"],
    "A": [1,2,3],
    "B": [4,5,6]
}

df = pd.DataFrame(data=my_dict)

df["std"] = df.std(axis=1)

print(df)

Output:

0   x  1  4  2.12132
1   y  2  5  2.12132
2   z  3  6  2.12132

CodePudding user response:

Use std by row (axis=1):

df['stdDev'] = df[['A', 'B', 'C', 'D']].std(axis=1)

output:

  Key  A  B   C   D    stdDev
0   X  1  2   3   4  1.290994
1   y  4  5   6   7  1.290994
2   z  8  9  10  11  1.290994
  • Related