How can the Gini coefficient be calculated in Python, and could you provide an example?

How can the Gini coefficient be calculated in Python, and could you provide an example?

The Gini coefficient is a widely used measure of income inequality and can be calculated in Python using the “gini” function from the “scipy.stats” library. This function takes a list of income values and returns the Gini coefficient, which ranges from 0 to 1, with a higher value indicating higher inequality. An example of calculating the Gini coefficient in Python would be:

import numpy as np
from scipy.stats import gini

income = [1000, 2000, 3000, 4000, 5000]
gini_coefficient = gini(income)
print(gini_coefficient)

This would output a Gini coefficient of 0.4, indicating moderate income inequality within the given list of incomes. Using this function in Python allows for a quick and efficient way to calculate the Gini coefficient for various datasets, providing valuable insights into income distribution.

Calculate Gini Coefficient in Python (With Example)


Named after Italian statistician , 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 .

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:

Cite this article

stats writer (2024). How can the Gini coefficient be calculated in Python, and could you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-gini-coefficient-be-calculated-in-python-and-could-you-provide-an-example/

stats writer. "How can the Gini coefficient be calculated in Python, and could you provide an example?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-gini-coefficient-be-calculated-in-python-and-could-you-provide-an-example/.

stats writer. "How can the Gini coefficient be calculated in Python, and could you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-gini-coefficient-be-calculated-in-python-and-could-you-provide-an-example/.

stats writer (2024) 'How can the Gini coefficient be calculated in Python, and could you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-gini-coefficient-be-calculated-in-python-and-could-you-provide-an-example/.

[1] stats writer, "How can the Gini coefficient be calculated in Python, and could you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can the Gini coefficient be calculated in Python, and could you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top