Search code examples
pythonrpandaspandas-groupbypsych

Group by function with pandas dataset. Cronbach's alpha with grouped dataset in Python


Let's say I have a dataset (sim_data) with 16 variables, including psychological data (15 items from a questionnaire), and the first column is a categorical variable (country).

I can easily get means/sd by group using:

sim_data.groupby("country").describe()

However, I would like to apply a function (Cronbach's alpha) from a specific package (pip install pingouin) and (import pingouin as pg) with this data and get the results by group (as I did previously). The following code is not working.

pg.cronbach_alpha(sim_data.groupby("country"))

Neither this one

sim_data.groupby('country').apply(lambda grp: pg.cronbach_alpha())

Important notes:

I'm adapting the code from here

That's my routine and dataset if you want to reproduce

import pandas as pd
import numpy as np
import pingouin as pg
from numpy import nan

sim_data = pd.DataFrame.from_dict({'country': {33003: 'Vietnam',
  12172: 'Macedonia',
  5192: 'Ghana',
  32511: 'Vietnam',
  7813: 'Ghana',
  21440: 'Armenia',
  32912: 'Vietnam',
  20609: 'Georgia',
  7751: 'Ghana',
  31228: 'Laos',
  36230: 'Serbia',
  32025: 'Laos',
  21758: 'Armenia',
  35730: 'Serbia',
  8444: 'Kenya',
  19946: 'Georgia',
  37532: 'Serbia',
  22926: 'Armenia',
  27204: 'Sri_Lanka',
  31959: 'Laos',
  24535: 'Sri_Lanka',
  19433: 'Georgia',
  17137: 'Ukraine',
  26103: 'Sri_Lanka',
  33878: 'Vietnam',
  39053: 'Philippines',
  33799: 'Vietnam',
  37183: 'Serbia',
  3606: 'Colombia',
  2561: 'Colombia',
  29960: 'Laos',
  32397: 'Vietnam',
  39539: 'Philippines',
  431: 'Bolivia',
  36842: 'Serbia',
  16926: 'Ukraine',
  9287: 'Kenya',
  34246: 'Vietnam',
  16277: 'Ukraine',
  34105: 'Vietnam',
  11032: 'Kenya',
  41457: 'Philippines',
  34380: 'Vietnam',
  19482: 'Georgia',
  1844: 'Bolivia',
  9552: 'Kenya',
  35437: 'Vietnam',
  29555: 'Laos',
  29993: 'Laos',
  28114: 'Yunnan',
  15: 'Bolivia',
  27712: 'Yunnan',
  12313: 'Macedonia',
  11631: 'Kenya'},
 'step_bfi1_ab_cor': {33003: 1.8333332538604736,
  12172: 1.5,
  5192: nan,
  32511: 1.4166666269302368,
  7813: nan,
  21440: 2.4166665077209473,
  32912: 1.0,
  20609: 4.0,
  7751: 0.5,
  31228: 2.25,
  36230: 2.9375,
  32025: 1.875,
  21758: 3.9166665077209473,
  35730: 2.25,
  8444: 3.5833334922790527,
  19946: 2.3333334922790527,
  37532: 4.0625,
  22926: 3.5,
  27204: 4.083333492279053,
  31959: 3.0,
  24535: 3.5,
  19433: 2.0833334922790527,
  17137: 3.25,
  26103: 1.1666666269302368,
  33878: 1.6666667461395264,
  39053: 1.75,
  33799: 0.75,
  37183: 3.375,
  3606: 4.25,
  2561: 2.8333334922790527,
  29960: 4.25,
  32397: 3.8333332538604736,
  39539: 2.5625,
  431: 3.25,
  36842: 3.8125,
  16926: 1.9166667461395264,
  9287: 2.3333334922790527,
  34246: 1.0,
  16277: 2.9166667461395264,
  34105: 1.5833333730697632,
  11032: 1.75,
  41457: 3.75,
  34380: 1.5833332538604736,
  19482: 0.9166666269302368,
  1844: 3.6666667461395264,
  9552: 2.25,
  35437: 1.0833332538604736,
  29555: 2.25,
  29993: 4.25,
  28114: 3.6666665077209473,
  15: 3.0,
  27712: 3.25,
  12313: 2.0833334922790527,
  11631: 2.5833334922790527},
 'step_bfi3_ab_cor': {33003: 3.8333332538604736,
  12172: 3.5,
  5192: nan,
  32511: 3.4166665077209473,
  7813: nan,
  21440: 3.4166665077209473,
  32912: 3.0,
  20609: 4.0,
  7751: 2.5,
  31228: 4.25,
  36230: 3.6875,
  32025: 3.875,
  21758: 3.9166665077209473,
  35730: 2.25,
  8444: 4.583333492279053,
  19946: 2.3333334922790527,
  37532: 2.5625,
  22926: 2.5,
  27204: 3.0833334922790527,
  31959: 4.0,
  24535: 2.5,
  19433: 3.0833334922790527,
  17137: 3.25,
  26103: 3.1666665077209473,
  33878: 3.6666667461395264,
  39053: 2.5,
  33799: 3.75,
  37183: 3.375,
  3606: 4.25,
  2561: 2.8333334922790527,
  29960: 3.25,
  32397: 2.8333332538604736,
  39539: 2.5625,
  431: 3.25,
  36842: 3.0625,
  16926: 3.9166667461395264,
  9287: 3.3333334922790527,
  34246: 4.0,
  16277: 1.9166667461395264,
  34105: 3.5833334922790527,
  11032: 3.75,
  41457: 2.25,
  34380: 2.5833332538604736,
  19482: 3.9166665077209473,
  1844: 2.6666667461395264,
  9552: 3.25,
  35437: 3.0833332538604736,
  29555: 2.25,
  29993: 2.25,
  28114: 3.6666665077209473,
  15: 3.0,
  27712: 2.25,
  12313: 4.083333492279053,
  11631: 3.5833334922790527},
 'step_bfi5_ab_cor': {33003: 0.8333332538604736,
  12172: 0.5,
  5192: nan,
  32511: 2.4166665077209473,
  7813: nan,
  21440: 2.4166665077209473,
  32912: 2.0,
  20609: 2.0,
  7751: 0.5,
  31228: 3.25,
  36230: 3.6875,
  32025: 1.875,
  21758: 3.9166665077209473,
  35730: 2.25,
  8444: 1.5833333730697632,
  19946: 1.3333333730697632,
  37532: 1.8125,
  22926: 1.5,
  27204: 2.0833334922790527,
  31959: 3.0,
  24535: 1.5,
  19433: 1.0833333730697632,
  17137: 2.25,
  26103: 4.166666507720947,
  33878: 1.6666667461395264,
  39053: 1.75,
  33799: 1.75,
  37183: 3.375,
  3606: 2.25,
  2561: 1.8333333730697632,
  29960: 3.25,
  32397: 1.8333332538604736,
  39539: 2.5625,
  431: 1.25,
  36842: 3.8125,
  16926: 1.9166667461395264,
  9287: 1.3333333730697632,
  34246: 3.0,
  16277: 1.9166667461395264,
  34105: 1.5833333730697632,
  11032: 2.75,
  41457: 2.25,
  34380: 2.5833332538604736,
  19482: 1.9166666269302368,
  1844: 3.6666667461395264,
  9552: 1.25,
  35437: 1.0833332538604736,
  29555: 1.25,
  29993: 3.25,
  28114: 2.6666665077209473,
  15: 2.0,
  27712: 3.25,
  12313: 1.0833333730697632,
  11631: 3.5833334922790527},
 'step_bfi6_ab_cor': {33003: 4.1666669845581055,
  12172: 1.5,
  5192: nan,
  32511: 1.5833333730697632,
  7813: nan,
  21440: 2.5833334922790527,
  32912: 4.0,
  20609: 3.0,
  7751: 1.5,
  31228: 2.75,
  36230: 2.8125,
  32025: 4.125,
  21758: 4.083333492279053,
  35730: 1.25,
  8444: 3.4166665077209473,
  19946: 2.6666665077209473,
  37532: 3.9375,
  22926: 2.5,
  27204: 2.9166665077209473,
  31959: 2.0,
  24535: 2.5,
  19433: 2.9166665077209473,
  17137: 2.75,
  26103: 0.8333333730697632,
  33878: 4.3333330154418945,
  39053: 2.5,
  33799: 4.25,
  37183: 3.125,
  3606: 2.75,
  2561: 3.1666665077209473,
  29960: 3.75,
  32397: 1.1666667461395264,
  39539: 3.9375,
  431: 1.75,
  36842: 2.6875,
  16926: 2.0833332538604736,
  9287: 2.6666665077209473,
  34246: 3.0,
  16277: 2.0833332538604736,
  34105: 2.4166665077209473,
  11032: 3.25,
  41457: 4.25,
  34380: 2.4166667461395264,
  19482: 4.083333492279053,
  1844: 3.3333332538604736,
  9552: 2.75,
  35437: 2.9166667461395264,
  29555: 1.75,
  29993: 2.75,
  28114: 3.3333334922790527,
  15: 2.0,
  27712: 1.75,
  12313: 2.9166665077209473,
  11631: 3.4166665077209473},
 'step_bfi7_ab_cor': {33003: 1.8333332538604736,
  12172: 1.5,
  5192: nan,
  32511: 1.4166666269302368,
  7813: nan,
  21440: 2.4166665077209473,
  32912: 3.0,
  20609: 2.0,
  7751: 1.5,
  31228: 3.25,
  36230: 3.6875,
  32025: 1.875,
  21758: 3.9166665077209473,
  35730: 3.75,
  8444: 2.5833334922790527,
  19946: 1.3333333730697632,
  37532: 4.0625,
  22926: 2.5,
  27204: 3.0833334922790527,
  31959: 1.0,
  24535: 1.5,
  19433: 2.0833334922790527,
  17137: 2.25,
  26103: 2.1666665077209473,
  33878: 2.6666667461395264,
  39053: 3.25,
  33799: 0.75,
  37183: 3.375,
  3606: 2.25,
  2561: 1.8333333730697632,
  29960: 2.25,
  32397: 3.8333332538604736,
  39539: 4.0625,
  431: 3.25,
  36842: 3.8125,
  16926: 1.9166667461395264,
  9287: 2.3333334922790527,
  34246: 2.0,
  16277: 2.9166667461395264,
  34105: 1.5833333730697632,
  11032: 1.75,
  41457: 3.75,
  34380: 2.5833332538604736,
  19482: 2.9166665077209473,
  1844: 1.6666667461395264,
  9552: 2.25,
  35437: 2.0833332538604736,
  29555: 2.25,
  29993: 2.25,
  28114: 3.6666665077209473,
  15: 3.0,
  27712: 1.25,
  12313: 4.083333492279053,
  11631: 3.5833334922790527},
 'step_bfi10_ab_cor': {33003: 3.8333332538604736,
  12172: 3.5,
  5192: nan,
  32511: 3.4166665077209473,
  7813: nan,
  21440: 3.4166665077209473,
  32912: 3.0,
  20609: 4.0,
  7751: 3.5,
  31228: 3.25,
  36230: 3.6875,
  32025: 1.875,
  21758: 3.9166665077209473,
  35730: 2.25,
  8444: 2.5833334922790527,
  19946: 4.333333492279053,
  37532: 2.5625,
  22926: 2.5,
  27204: 4.083333492279053,
  31959: 2.0,
  24535: 0.4999999403953552,
  19433: 3.0833334922790527,
  17137: 3.25,
  26103: 3.1666665077209473,
  33878: 2.6666667461395264,
  39053: 3.25,
  33799: 1.75,
  37183: 3.375,
  3606: 2.25,
  2561: 2.8333334922790527,
  29960: 2.25,
  32397: 1.8333332538604736,
  39539: 4.0625,
  431: 3.25,
  36842: 3.0625,
  16926: 3.9166667461395264,
  9287: 2.3333334922790527,
  34246: 4.0,
  16277: 2.9166667461395264,
  34105: 2.5833334922790527,
  11032: 3.75,
  41457: 2.25,
  34380: 3.5833332538604736,
  19482: 3.9166665077209473,
  1844: 3.6666667461395264,
  9552: 3.25,
  35437: 3.0833332538604736,
  29555: 2.25,
  29993: 2.25,
  28114: 3.6666665077209473,
  15: 3.0,
  27712: 3.25,
  12313: 2.0833334922790527,
  11631: 4.583333492279053},
 'step_bfi17_ab_cor': {33003: 2.8333332538604736,
  12172: 1.5,
  5192: nan,
  32511: 3.4166665077209473,
  7813: nan,
  21440: 1.4166666269302368,
  32912: 2.0,
  20609: 1.0,
  7751: 3.5,
  31228: 3.25,
  36230: 2.1875,
  32025: nan,
  21758: 3.9166665077209473,
  35730: 3.75,
  8444: 3.5833334922790527,
  19946: 2.3333334922790527,
  37532: 2.5625,
  22926: 2.5,
  27204: 3.0833334922790527,
  31959: 3.0,
  24535: 2.5,
  19433: 4.083333492279053,
  17137: 2.25,
  26103: 2.1666665077209473,
  33878: 2.6666667461395264,
  39053: 1.75,
  33799: 2.75,
  37183: 3.375,
  3606: 4.25,
  2561: 0.8333333730697632,
  29960: 3.25,
  32397: 1.8333332538604736,
  39539: 2.5625,
  431: 1.25,
  36842: 2.3125,
  16926: 2.9166667461395264,
  9287: 3.3333334922790527,
  34246: 2.0,
  16277: 2.9166667461395264,
  34105: 3.5833334922790527,
  11032: 2.75,
  41457: 3.75,
  34380: 2.5833332538604736,
  19482: 3.9166665077209473,
  1844: 3.6666667461395264,
  9552: 3.25,
  35437: 2.0833332538604736,
  29555: 2.25,
  29993: 2.25,
  28114: 3.6666665077209473,
  15: 3.0,
  27712: 2.25,
  12313: 3.0833334922790527,
  11631: 3.5833334922790527},
 'step_bfi19_ab_cor': {33003: 3.1666667461395264,
  12172: 1.5,
  5192: nan,
  32511: 1.5833333730697632,
  7813: nan,
  21440: 2.5833334922790527,
  32912: 2.0,
  20609: 1.0,
  7751: 1.5,
  31228: 2.75,
  36230: 2.0625,
  32025: 2.125,
  21758: 1.0833333730697632,
  35730: 4.25,
  8444: 3.4166665077209473,
  19946: 3.6666665077209473,
  37532: 0.9375,
  22926: 2.5,
  27204: 2.9166665077209473,
  31959: 3.0,
  24535: 1.5,
  19433: 2.9166665077209473,
  17137: 2.75,
  26103: 1.8333333730697632,
  33878: 3.3333332538604736,
  39053: 3.25,
  33799: 3.25,
  37183: 3.125,
  3606: 2.75,
  2561: 3.1666665077209473,
  29960: 1.75,
  32397: 3.1666667461395264,
  39539: 2.4375,
  431: 1.75,
  36842: 2.6875,
  16926: 3.0833332538604736,
  9287: 4.666666507720947,
  34246: 3.0,
  16277: 2.0833332538604736,
  34105: 3.4166665077209473,
  11032: 3.25,
  41457: 2.75,
  34380: 2.4166667461395264,
  19482: 3.0833334922790527,
  1844: 4.3333330154418945,
  9552: 2.75,
  35437: 2.9166667461395264,
  29555: 1.75,
  29993: 1.75,
  28114: 3.3333334922790527,
  15: 2.0,
  27712: 2.75,
  12313: 0.9166666269302368,
  11631: 2.4166665077209473},
 'step_bfi23_ab_cor': {33003: 1.1666667461395264,
  12172: 3.5,
  5192: nan,
  32511: 4.583333492279053,
  7813: nan,
  21440: 3.5833334922790527,
  32912: 2.0,
  20609: 4.0,
  7751: 4.5,
  31228: 2.75,
  36230: 3.5625,
  32025: nan,
  21758: 4.083333492279053,
  35730: 4.25,
  8444: 3.4166665077209473,
  19946: 3.6666665077209473,
  37532: 3.9375,
  22926: 3.5,
  27204: 2.9166665077209473,
  31959: 3.0,
  24535: 2.5,
  19433: 2.9166665077209473,
  17137: 2.75,
  26103: 3.8333334922790527,
  33878: 1.3333332538604736,
  39053: 1.75,
  33799: 2.25,
  37183: 4.625,
  3606: 2.75,
  2561: 3.1666665077209473,
  29960: 2.75,
  32397: 3.1666667461395264,
  39539: 2.4375,
  431: 3.75,
  36842: 3.4375,
  16926: 3.0833332538604736,
  9287: 1.6666666269302368,
  34246: 3.0,
  16277: 3.0833332538604736,
  34105: 1.4166666269302368,
  11032: 4.25,
  41457: 2.75,
  34380: 2.4166667461395264,
  19482: 4.083333492279053,
  1844: 3.3333332538604736,
  9552: 3.75,
  35437: 1.9166667461395264,
  29555: 2.75,
  29993: 2.75,
  28114: 3.3333334922790527,
  15: 4.0,
  27712: 2.75,
  12313: 2.9166665077209473,
  11631: 3.4166665077209473},
 'step_bfi30_ab_cor': {33003: 1.8333332538604736,
  12172: 3.5,
  5192: nan,
  32511: 2.4166665077209473,
  7813: nan,
  21440: 3.4166665077209473,
  32912: 4.0,
  20609: 4.0,
  7751: 3.5,
  31228: 3.25,
  36230: 3.6875,
  32025: 1.875,
  21758: 3.9166665077209473,
  35730: 2.25,
  8444: 2.5833334922790527,
  19946: 4.333333492279053,
  37532: 4.0625,
  22926: 3.5,
  27204: 4.083333492279053,
  31959: 4.0,
  24535: 3.5,
  19433: 4.083333492279053,
  17137: 3.25,
  26103: 3.1666665077209473,
  33878: 3.6666667461395264,
  39053: 1.0,
  33799: 0.75,
  37183: 3.375,
  3606: 4.25,
  2561: 2.8333334922790527,
  29960: 3.25,
  32397: 2.8333332538604736,
  39539: 4.0625,
  431: 3.25,
  36842: 3.8125,
  16926: 2.9166667461395264,
  9287: 2.3333334922790527,
  34246: 3.0,
  16277: 2.9166667461395264,
  34105: 2.5833334922790527,
  11032: 3.75,
  41457: 3.75,
  34380: 3.5833332538604736,
  19482: 3.9166665077209473,
  1844: 3.6666667461395264,
  9552: 2.25,
  35437: 3.0833332538604736,
  29555: 2.25,
  29993: 3.25,
  28114: 3.6666665077209473,
  15: 1.0,
  27712: 3.25,
  12313: 4.083333492279053,
  11631: 4.583333492279053},
 'step_bfi32_ab_cor': {33003: 3.8333332538604736,
  12172: 1.5,
  5192: nan,
  32511: 3.4166665077209473,
  7813: nan,
  21440: 3.4166665077209473,
  32912: 4.0,
  20609: 4.0,
  7751: 3.5,
  31228: 3.25,
  36230: 2.9375,
  32025: 2.875,
  21758: 3.9166665077209473,
  35730: 2.25,
  8444: 3.5833334922790527,
  19946: 4.333333492279053,
  37532: 3.3125,
  22926: 3.5,
  27204: 4.083333492279053,
  31959: 3.0,
  24535: 2.5,
  19433: 4.083333492279053,
  17137: 3.25,
  26103: 3.1666665077209473,
  33878: 3.6666667461395264,
  39053: 1.75,
  33799: 3.75,
  37183: 1.875,
  3606: 4.25,
  2561: 2.8333334922790527,
  29960: 3.25,
  32397: 2.8333332538604736,
  39539: 2.5625,
  431: 3.25,
  36842: 3.0625,
  16926: 2.9166667461395264,
  9287: 2.3333334922790527,
  34246: 4.0,
  16277: 2.9166667461395264,
  34105: 3.5833334922790527,
  11032: 3.75,
  41457: 2.25,
  34380: 2.5833332538604736,
  19482: 3.9166665077209473,
  1844: 3.6666667461395264,
  9552: 3.25,
  35437: 3.0833332538604736,
  29555: 3.25,
  29993: 3.25,
  28114: 3.6666665077209473,
  15: 3.0,
  27712: 2.25,
  12313: 3.0833334922790527,
  11631: 3.5833334922790527},
 'step_bfi33_ab_cor': {33003: 1.8333332538604736,
  12172: 3.5,
  5192: nan,
  32511: 3.4166665077209473,
  7813: nan,
  21440: 2.4166665077209473,
  32912: 4.0,
  20609: 2.0,
  7751: 3.5,
  31228: 3.25,
  36230: 2.9375,
  32025: 3.875,
  21758: 3.9166665077209473,
  35730: 3.75,
  8444: 3.5833334922790527,
  19946: 4.333333492279053,
  37532: 4.0625,
  22926: 2.5,
  27204: 3.0833334922790527,
  31959: 2.0,
  24535: 1.5,
  19433: 2.0833334922790527,
  17137: 1.25,
  26103: 3.1666665077209473,
  33878: 3.6666667461395264,
  39053: 2.5,
  33799: 3.75,
  37183: 3.375,
  3606: 2.25,
  2561: 2.8333334922790527,
  29960: 3.25,
  32397: 2.8333332538604736,
  39539: 3.3125,
  431: 2.25,
  36842: 3.0625,
  16926: 2.9166667461395264,
  9287: 3.3333334922790527,
  34246: 3.0,
  16277: 2.9166667461395264,
  34105: 3.5833334922790527,
  11032: 3.75,
  41457: 3.75,
  34380: 3.5833332538604736,
  19482: 3.9166665077209473,
  1844: 3.6666667461395264,
  9552: 2.25,
  35437: 3.0833332538604736,
  29555: 3.25,
  29993: 2.25,
  28114: 3.6666665077209473,
  15: 3.0,
  27712: 1.25,
  12313: 3.0833334922790527,
  11631: 3.5833334922790527},
 'step_bfi34_ab_cor': {33003: 3.8333332538604736,
  12172: 1.5,
  5192: nan,
  32511: 3.4166665077209473,
  7813: nan,
  21440: 3.4166665077209473,
  32912: 3.0,
  20609: 1.0,
  7751: 3.5,
  31228: 2.25,
  36230: 2.9375,
  32025: nan,
  21758: 3.9166665077209473,
  35730: 3.75,
  8444: 2.5833334922790527,
  19946: 3.3333334922790527,
  37532: 2.5625,
  22926: 2.5,
  27204: 2.0833334922790527,
  31959: 2.0,
  24535: 1.5,
  19433: 3.0833334922790527,
  17137: 3.25,
  26103: 2.1666665077209473,
  33878: 2.6666667461395264,
  39053: 2.5,
  33799: 3.75,
  37183: 3.375,
  3606: 1.25,
  2561: 3.8333334922790527,
  29960: 2.25,
  32397: 1.8333332538604736,
  39539: 2.5625,
  431: 2.25,
  36842: 2.3125,
  16926: 2.9166667461395264,
  9287: 3.3333334922790527,
  34246: 4.0,
  16277: 2.9166667461395264,
  34105: 1.5833333730697632,
  11032: 3.75,
  41457: 2.25,
  34380: 1.5833332538604736,
  19482: 3.9166665077209473,
  1844: 3.6666667461395264,
  9552: 3.25,
  35437: 3.0833332538604736,
  29555: 2.25,
  29993: 2.25,
  28114: 2.6666665077209473,
  15: 3.0,
  27712: 2.25,
  12313: 1.0833333730697632,
  11631: 2.5833334922790527},
 'step_bfi36_ab_cor': {33003: 2.8333332538604736,
  12172: 3.5,
  5192: nan,
  32511: 3.4166665077209473,
  7813: nan,
  21440: 2.4166665077209473,
  32912: 3.0,
  20609: 4.0,
  7751: 2.5,
  31228: 3.25,
  36230: 3.6875,
  32025: 1.875,
  21758: 1.9166666269302368,
  35730: 2.25,
  8444: 3.5833334922790527,
  19946: 4.333333492279053,
  37532: 3.3125,
  22926: 3.5,
  27204: 3.0833334922790527,
  31959: 3.0,
  24535: 2.5,
  19433: 4.083333492279053,
  17137: 2.25,
  26103: 2.1666665077209473,
  33878: 3.6666667461395264,
  39053: 3.25,
  33799: 3.75,
  37183: 3.375,
  3606: 4.25,
  2561: 2.8333334922790527,
  29960: 3.25,
  32397: 1.8333332538604736,
  39539: 2.5625,
  431: 2.25,
  36842: 3.0625,
  16926: 1.9166667461395264,
  9287: 0.3333333730697632,
  34246: 2.0,
  16277: 2.9166667461395264,
  34105: 3.5833334922790527,
  11032: 3.75,
  41457: 3.75,
  34380: 3.5833332538604736,
  19482: 3.9166665077209473,
  1844: 3.6666667461395264,
  9552: 3.25,
  35437: 3.0833332538604736,
  29555: 1.25,
  29993: 2.25,
  28114: 3.6666665077209473,
  15: 3.0,
  27712: 3.25,
  12313: 4.083333492279053,
  11631: 3.5833334922790527},
 'step_bfi39_ab_cor': {33003: 4.1666669845581055,
  12172: 3.5,
  5192: nan,
  32511: 4.583333492279053,
  7813: nan,
  21440: 2.5833334922790527,
  32912: 3.0,
  20609: 1.0,
  7751: 2.5,
  31228: 3.75,
  36230: 4.3125,
  32025: 1.125,
  21758: 4.083333492279053,
  35730: 2.75,
  8444: 3.4166665077209473,
  19946: 3.6666665077209473,
  37532: 2.4375,
  22926: 2.5,
  27204: 2.9166665077209473,
  31959: 3.0,
  24535: 1.5,
  19433: 2.9166665077209473,
  17137: 2.75,
  26103: 2.8333334922790527,
  33878: 3.3333332538604736,
  39053: 3.25,
  33799: 3.25,
  37183: 1.625,
  3606: 3.75,
  2561: 3.1666665077209473,
  29960: 3.75,
  32397: 3.1666667461395264,
  39539: 0.9375,
  431: 2.75,
  36842: 2.6875,
  16926: 3.0833332538604736,
  9287: 2.6666665077209473,
  34246: 3.0,
  16277: 4.0833330154418945,
  34105: 4.416666507720947,
  11032: 2.25,
  41457: 1.25,
  34380: 2.4166667461395264,
  19482: 1.0833333730697632,
  1844: 3.3333332538604736,
  9552: 1.75,
  35437: 3.9166667461395264,
  29555: 2.75,
  29993: 2.75,
  28114: 3.3333334922790527,
  15: 4.0,
  27712: 2.75,
  12313: 2.9166665077209473,
  11631: 2.4166665077209473}})

I'm a heavy R user and and I'm translating the following code to python

sim_data %>%
  select('step_bfi1_ab_cor':'step_bfi39_ab_cor', "country") %>%
  nest(-country) %>%
  mutate(result=map(data, ~psych::alpha(.)$total)) %>%
  select(country,result) %>%
  unnest()

Suggestions are welcome. Please let me know if there's another way (more elegant) to solve my problem. Thank you


Solution

  • Usually, base R (not tidy R) is easier to translate to Python Pandas. What your R code appears to be doing is subsetting the dataframe by country column and running each subset into psych::alpha(). Then extracted statistics are returned into a data frame with country indicator.

    You can do the exact same with base R's by which can translate into Pandas' groupby in a list comprehension. It looks like psych::alpha returns more statistics than pingouin.cronbach_alpha. Adjust fields and return values below as needed in untested code.

    Base R (using docs)

    # DEFINE METHOD
    run_cronbach_alpha <- function(sub) {
        results <- psych::alpha(sub)$total    # RETURNS LIST
                          
        data.frame(country = sub$country[1],
                   raw_alpha = results$raw_alpha,
                   std.alpha = results$std.alpha,
                   G6 = results$G6,
                   average_r = results$average_r,
                   median_r = results$median,
                   mean = results$mean,
                   sd = results$sd)
     }
    
    
    # FILTER COLUMNS IN DATA FRAME
    sim_short <- sim_data[c("step_bfi1_ab_cor", ..., "step_bfi39_ab_cor", "country")] 
    
    # RUN METHOD BY COUNTRY SUBSETS TO RETURN DF LIST
    results_df_list <- by(sim_short, sim_short$country, run_cronbach_alpha)
    
    # ROW BIND ALL DFs TO SINGLE FINAL DATA FRAME
    results_df <- do.call(rbind.data.frame, results_df_list)
    

    Python Pandas (using docs)

    # DEFINE METHOD
    def run_cronbach_alpha(c, sub):
        results = pg.cronbach_alpha(sub.drop(["country"], axis="columns"))    # RETURNS TUPLE
    
        return pd.DataFrame({'country': c, 'cronbach_alpha': results[0], index=[0]})
    
    
    # FILTER COLUMNS IN DATA FRAME
    sim_short = sim_data.reindex(["step_bfi1_ab_cor", ..., "step_bfi39_ab_cor", "country"], 
                                 axis='columns')
                             
    # RUN METHOD BY COUNTRY SUBSETS TO RETURN DF LIST
    results_df_list = [run_cronbach_alpha(i, df) for i,df in sim_short.groupby("country")]
    
    # CONCATENATE ALL DFs TO SINGLE FINAL DATA FRAME
    results_df = pd.concat(results_df_list)