Home > Mobile >  calculate aggregated variance for each group in python
calculate aggregated variance for each group in python

Time:12-07

I have a data frame (df) with these columns: user, vector, and group.

df = pd.DataFrame({'user': ['user_1', 'user_2', 'user_3', 'user_4', 'user_5',  'user_6'], 'vector': [[1, 0, 2, 0], [1, 8, 0, 2],[6, 2, 0, 0], [5, 0, 2, 2], [3, 8, 0, 0],[6, 0, 0, 2]], 'group': ['A', 'B', 'C', 'B', 'A', 'A']})

I want to calculate aggregated variance for each group.

I tried this code, but it return an error

aggregated_variance = (df.groupby('group', as_index=False)['vector'].agg(["var"]))

ValueError: no results

CodePudding user response:

If you take the sum() after you group df, you will have a dataframe that shows a list of all vector values for each group. Then, create a lambda function to calculate the variance of each list of vector values.

aggregated = df.groupby("group").sum()['vector']
aggregated_variance = aggregated.apply(lambda x: np.var(x)).reset_index()

CodePudding user response:

You can use .explode to clean up your data and then perform a .groupby operation:

out = (
    df.explode('vector')
    .groupby('group')['vector'].var(ddof=1)
)

print(out)
group
A    7.060606
B    7.428571
C    8.000000
Name: vector, dtype: float64

The trick here lies in the use of .explode:

>>> df.head()
     user        vector group
0  user_1  [1, 0, 2, 0]     A
1  user_2  [1, 8, 0, 2]     B
2  user_3  [6, 2, 0, 0]     C
3  user_4  [5, 0, 2, 2]     B
4  user_5  [3, 8, 0, 0]     A

>>> df.explode('vector').head()
     user vector group
0  user_1      1     A
0  user_1      0     A
0  user_1      2     A
0  user_1      0     A
1  user_2      1     B
...

CodePudding user response:

    import pandas as pd
    
    # Create a DataFrame with the data you provided
    df = pd.DataFrame({'user': ['user_1', 'user_2', 'user_3', 'user_4', 'user_5', 'user_6'],
                       'vector': [[1, 0, 2, 0], [1, 8, 0, 2], [6, 2, 0, 0], [5, 0, 2, 2], [3, 8, 0, 0], [6, 0, 0, 2]],
                       'group': ['A', 'B', 'C', 'B', 'A', 'A']})
    
    # Group the data by the 'group' column and calculate the variance of the 'vector' column within each group
    aggregated_variance = df.groupby('group')['vector'].var()
    
    # Print the aggregated variance for each group
    print(aggregated_variance)

# Group the data by the 'group' column and calculate the variance of the 'vector' column within each group
aggregated_variance = df.groupby('group')['vector'].var()

# Move the group names from the index to a new column, and reset the index to be a range from 0 to the number of groups
aggregated_variance = aggregated_variance.reset_index()

# Print the resulting DataFrame
print(aggregated_variance)

CodePudding user response:

FIX: Here is the code for this solution:

import pandas as pd

# Storing the dataframe in a variable 
df = pd.DataFrame({'user': ['user_1', 'user_2', 'user_3', 'user_4', 'user_5',  'user_6'], 'vector': [[1, 0, 2, 0], [1, 8, 0, 2],[6, 2, 0, 0], [5, 0, 2, 2], [3, 8, 0, 0],[6, 0, 0, 2]], 'group': ['A', 'B', 'C', 'B', 'A', 'A']})

# Using the GroupBy function to reference the 'group' and the DataFrame's 'vector' columns 
grouped_data = df.groupby('group')['vector'].apply(lambda x: x.var())

# Printing out the resulting grouped variance
print(grouped_data)
  • Related