---
# System prepended metadata

title: 第十一堂課2022/5/9

---

# 第十一堂課2022/5/9

****課堂筆記****
****作業球員收入****

```python=
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
dfIncome = pd.read_excel('/content/drive/My Drive/___TestData/NBA球員收入.xlsx')
dfIncome
```
```python=
from google.colab import drive
drive.mount('/content/drive')
```
```python=
sum=dfIncome['Income'].sum()
n=dfIncome['Income'].count()
print(f'總和={sum}, 樣本數={n}')

#直方圖
plt.hist(dfIncome['收入'], bins=range(0, 15000, 1000))
plt.show()
```
```python=
dfLorenz=dfIncome[:]
se=dfLorenz['收入'].sort_values()
incomes=[0]
cumulativePercentage=[0]
cumulativeSum=[0]
n=se.count()
sum=se.sum()
cum=0
pr=0;
for _income in se:
  incomes.append(_income)
  pr+=1  
  cum+=_income  
  cumulativePercentage.append(pr/n)
  cumulativeSum.append(cum/sum)
integral=0

for i in range(1,n+1):
  integral+= (cumulativeSum[i-1]+cumulativeSum[i])*(1/n)/2
gini=(0.5-integral)/0.5
print(f'吉尼係數={gini:.8f}')
plt.plot([0,1],[0,1])
plt.plot(cumulativePercentage,cumulativeSum)

plt.axis('square')
plt.xlim(0,1)
plt.ylim(0,1)
print('羅倫茲曲線（Lorenz curve）')
plt.show()
