Home » How to Calculate Gini Coefficient in Python (With Example)

How to Calculate Gini Coefficient in Python (With Example)

by Erma Khan

Named after Italian statistician Corrado Gini, the Gini coefficient is a way to measure the income distribution of a population.

The value for the Gini coefficient ranges from 0 to 1 where higher values represent greater income inequality and where:

  • 0 represents perfect income equality (everyone has the same income)
  • 1 represents perfect income inequality (one individual has all the income)

You can find a list of Gini coefficients by country here.

The following example shows how to calculate a Gini coefficient in Python.

Example: Calculate Gini Coefficient in Python

To calculate a Gini coefficient in Python, we’ll need to first define a simple function to calculate a Gini coefficient for a NumPy array of values:

import numpy as np

#define function to calculate Gini coefficient
def gini(x):
    total = 0
    for i, xi in enumerate(x[:-1], 1):
        total += np.sum(np.abs(xi - x[i:]))
    return total / (len(x)**2 * np.mean(x))

Next, we’ll use this function to calculate a Gini coefficient for an array of income values.

For example, suppose we have the following list of annual incomes for 10 individuals:

Income: $50k, $50k, $70k, $70k, $70k, $90k, $150k, $150k, $150k, $150k

The following code shows how to use the gini() function we just created to calculate the Gini coefficient for this population:

#define NumPy array of income values
incomes = np.array([50, 50, 70, 70, 70, 90, 150, 150, 150, 150])

#calculate Gini coefficient for array of incomes
gini(incomes)

0.226

The Gini coefficient turns out to be 0.226.

Note: In a real-world scenario there would be hundreds of thousands of different incomes for individuals in a certain country, but in this example we used 10 individuals as a simple illustration.

Additional Resources

The following tutorials explain how to calculate a Gini coefficient using different statistical software:

How to Calculate Gini Coefficient in R
How to Calculate Gini Coefficient in Excel

Related Posts