Blog Post

Viva Insights Inspiration Library Articles
1 MIN READ

Analyse Person-level Metrics in Python

VI_Migration's avatar
VI_Migration
Silver Contributor
Oct 01, 2021

Here is a simple snippet for analysing person-level means for Workplace Analytics metrics using `pandas`.

This example is based on running Python via R and the `reticulate` R package, so the data is actually passed on to `our_data_py` from the object `r.our_data`, but this can be imported using a CSV file. 

import pandas as pd
import dateutil

our_data_py = r.our_data

# our_data_py.head()

# our_data_py.shape

our_data_py.info()

We can do a bit of analysis with it. Let's start with changing the date:

our_data_py['Date'] = our_data_py['Date'].apply(dateutil.parser.parse, dayfirst = True)
our_data_py['Date'].describe()

The next step is to create a function that aggregates metrics at the person and org level:

def analyse_metric(data, metric, hrvar):

data = data.groupby(['PersonId',hrvar])
data = data[metric].mean()
data = data.reset_index()
data = data.groupby(hrvar)
output = data[metric].mean()

return output

analyse_metric(our_data_py, metric = 'Collaboration_hours', hrvar = 'LevelDesignation')



Published Oct 01, 2021
Version 1.0

1 Comment

  • VI_Migration's avatar
    VI_Migration
    Silver Contributor

    Here is a cleaned-up example on how to load in a csv file and to run the above person-averaging function: 

    import pandas as pd
    
    # path to person query
    sq_data = pd.read_csv('../data/demo spq.csv')
    
    # function for person-averaging
    def analyse_metric(data, metric, hrvar):
        data = data.groupby(['PersonId',hrvar])
        data = data[metric].mean()
        data = data.reset_index()
        data = data.groupby(hrvar)
        output = data[metric].mean()
        return output
    
    # run function
    analyse_metric(data = sq_data, metric = "Collaboration_hours", hrvar = "Organization")