cancel
Showing results for 
Search instead for 
Did you mean: 
Martin_Chan
Viva Expert
Viva Expert

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')

 

 



1 Comment
Martin_Chan
Viva Expert
Viva Expert

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")