Filter by string and then find variance of the other column

Viewed 291

I read my csv file using pandas and then just read these two columns

  • Describe_File

  • numbers

Describe_File   numbers
0   This is the start   25
1   Ending is coming    42
2   Middle of the story 525
3   This is the start   65
4   This is the start   25
5   Middle of the story 35
6   This is the start   28
7   This is the start   24
8   Ending is coming    24
9   Ending is coming    35
10  Ending is coming    25
11  Ending is coming    24
12  This is the start   215

So I filter now by a the string name ** This is the start** which looks like this

df = df[df.Describe_File == "This is the start"]

    Describe_File   numbers
0   This is the start   25
3   This is the start   65
4   This is the start   25
6   This is the start   28
7   This is the start   24
12  This is the start   21

And now I just find the variance np.var(df)

Goal

Go to Describe_File filter by the all the unique strings and then find the variance and standard deviation of that string.

The output file should look like this

enter image description here

1 Answers

As you know, the standard deviation is the square root of the variance. So the following would be the fastest way.

import pandas as pd
import numpy as np

df_out = df.groupby('Describe_File').apply(np.var)
df_out.columns = ['variance']
df_out['standard_deviation'] = np.sqrt(df_out['variance'])
Related