Python 如何按组应用函数?

Python 如何按组应用函数?,python,pandas,pandas-groupby,Python,Pandas,Pandas Groupby,我来自另一个讨论开始的地方。假设我有一个来自心理学研究的熊猫数据框架,其中有一个因子变量(国家),还有一些来自利克特量表的项目 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'

我来自另一个讨论开始的地方。假设我有一个来自心理学研究的熊猫数据框架,其中有一个因子变量(国家),还有一些来自利克特量表的项目

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}})
我定义了一个函数来执行Cronbach's Alpha分析,其代码如下:

def cronbach_alpha(df):
    # 1. Transform the df into a correlation matrix
    df_corr = df.corr()
    
    # 2.1 Calculate N
    # The number of variables equals the number of columns in the df
    N = df.shape[1]
    
    # 2.2 Calculate R
    # For this, we'll loop through the columns and append every
    # relevant correlation to an array calles "r_s". Then, we'll
    # calculate the mean of "r_s"
    rs = np.array([])
    for i, col in enumerate(df_corr.columns):
        sum_ = df_corr[col][i+1:].values
        rs = np.append(sum_, rs)
    mean_r = np.mean(rs)
    
   # 3. Use the formula to calculate Cronbach's Alpha 
    cronbach_alpha = (N * mean_r) / (1 + (N - 1) * mean_r)
    return cronbach_alpha
现在我很高兴,我可以使用下面的命令从我的物品中获得克朗巴赫的阿尔法:

cronbach_alpha(sim_data.drop("country",1))
然而,我希望所有国家都有克朗巴赫阿尔法。我所期望的结果与R的结果类似

如您所见,它对所有项目(来自此数据集的15个项目)运行Cronbach's alpha,但所有分析均按每个国家分组。我非常确信它可以用Python完成,并且开始帮助我。

  • 使用和功能
  • gana
    Nan
    ,因为该国家的数据框中只有一行有效数据
  • 当提供国家/地区的数据帧时,函数
    cronbach_alpha
    仅为该国家/地区返回1个值
cron=df.groupby('country',as_index=False)。apply(cronbach_alpha)。rename(columns={None:'val})
#显示(cron)
国家价值
0亚美尼亚0.918237
1玻利维亚0.751889
2哥伦比亚-80万
3格鲁吉亚0.238676
4加纳南部
5肯尼亚0.746892
6老挝0.752618
7马其顿-80万
8菲律宾0.569964
9塞尔维亚-0.675291
10斯里兰卡0.916891
11乌克兰-4.929153
12越南-0.668921
13云南0.977778
获取每个国家的描述性统计数据
df.groupby('country')。description()
[out]:
步骤1\u ab\u cor步骤3\u ab\u cor步骤5\u ab\u cor步骤6\u ab\u cor步骤7\u ab\u cor步骤10\u ab\u cor步骤17\u ab\u cor步骤19\u ab\u cor步骤23步骤30步骤32步骤33步骤步骤34\u ab\u cor步骤36\u ab\u cor步骤39\u ab\u cor
计数平均std最小值25%50%75%最大计数平均std最小值25%50%75%最大计数平均std最小值25%50%75%最大计数平均std最小值25%50%75%最大值计数平均std最小值25%50%75%最大计数平均std最小值25%50%75%最大计数平均std最小值25%50%75%最大计数平均std最小值25%50%75%最大值计数平均std最小值25%50%75%最大计数平均std最小值25%50%75%最大计数平均std最小值25%50%75%最大计数平均std最小值25%50%75%最大值计数平均标准最小值25%50%75%最大计数平均标准最小值25%50%75%最大计数平均标准最小值25%50%75%最大值
国家                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
亚美尼亚3.0 3.277778 0.774298 2.416667 2.958333 3.500000 3.708333 3.916667 3.0 3.277778 0.718473 2.500000 2.958333 3.416667 3.666667 3.916667 3.0 2.6111111 1.220011 1.500000 1.958333 2.416667 3.166667 3.916667 3.0 3.05555