R has the package “psych” which allows one to calculate the Cronbach’s alpha very easily just by one line:
psych::alpha(your_data, column_list)
For Python, the situation is more tricky since there does not seem to exist any package for calculating Cronbach’s alpha. Fortunately, the formula is not very complicated and it can be calculated in a few lines.
An existing code can be found on StackOverflow, but it has some small “bugs”. The corrected version is:
def CronbachAlpha(itemscores): itemscores = np.asarray(itemscores) itemvars = itemscores.var(axis=0, ddof=1) tscores = itemscores.sum(axis=1) nitems = itemscores.shape[1] return (nitems / (nitems-1)) * (1 - (itemvars.sum() / tscores.var(ddof=1)))
The input “itemscores” can be your Pandas DataFrame or any numpy array. (Note that this method requires you to “import numpy as np”).