---
# System prepended metadata

title: '0502'

---

# 0502
解吉尼係數
吉尼係數定義是在算分配不均的係數
吉尼係數=A/A+B

```
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

#程式追蹤方式：你可以逐步執行，並看看執行結果怎樣。

#讀取雲端硬碟中的excel檔
#雲端硬碟的路徑如何對應到/content/drive/My Drive/___DataSet/
#宣告一個變數成為特定的容器型態，在這個變數內可以存放很多值(數值、文字、物件)
#dfIncome = pd.read_excel('/content/drive/My Drive/___DataSet/001_Income_test.xlsx')
dfIncome = pd.read_excel('/content/drive/My Drive/___DataSet/001_Income_test.xlsx')
dfIncome
```
![](https://i.imgur.com/ZFaVHOu.jpg)

```
#總收入與人數
##https://www.w3schools.com/python/pandas/ref_df_count.asp
sum=dfIncome['Income'].sum() #income.sum欄位的加總 #dfIncome['Income']單獨抽出income欄位
n=dfIncome['Income'].count() #樣本數
print(f'總和={sum}, 樣本數={n}') #f宣告一個特殊字串，在{}內植入特定變數

#直方圖
plt.hist(dfIncome['Income'], bins=range(0, 10000, 1000))
plt.show()
```
![](https://i.imgur.com/CaFedXf.jpg)
```
dfIncome['Income'].count() #算個數(有幾筆紀錄)
```
```
dfIncome['Income'].sum() #算總和
```
```
dfLorenz=dfIncome[:]
se=dfLorenz['Income'].sort_values()
cumulativeSum=0
i=0
# 宣告xx為串列變數
xx=[]
# 宣告yy為串列變數
yy=[]
# 手動追蹤看看
for x in se:    
    i=i+1
    cumulativeSum+=x
    xx.append(i/n)
    yy.append(cumulativeSum/sum)
    print(f'第{i}次迴圈時 i={i} x={x} cumulativeSum={cumulativeSum} i/n={i/n} sum={sum} cumulativeSum/sum={cumulativeSum/sum} ')
    print(xx)
    print(yy)

plt.plot([0,1], linestyle = 'dotted') #線的呈現方式，點

#散佈圖
plt.scatter(x=xx,y=yy,s=0.1) #s=0.1點的大小
plt.axis('square') 
plt.xlim(0,1) #呈現x軸大小
plt.ylim(0,1) #呈現y軸大小

print('羅倫茲曲線（Lorenz curve）')
plt.show()

第1次迴圈時 i=1 x=1 cumulativeSum=1 i/n=0.1 sum=63 cumulativeSum/sum=0.015873015873015872 
[0.1]
[0.015873015873015872]
第2次迴圈時 i=2 x=1 cumulativeSum=2 i/n=0.2 sum=63 cumulativeSum/sum=0.031746031746031744 
[0.1, 0.2]
[0.015873015873015872, 0.031746031746031744]
第3次迴圈時 i=3 x=3 cumulativeSum=5 i/n=0.3 sum=63 cumulativeSum/sum=0.07936507936507936 
[0.1, 0.2, 0.3]
[0.015873015873015872, 0.031746031746031744, 0.07936507936507936]
第4次迴圈時 i=4 x=3 cumulativeSum=8 i/n=0.4 sum=63 cumulativeSum/sum=0.12698412698412698 
[0.1, 0.2, 0.3, 0.4]
[0.015873015873015872, 0.031746031746031744, 0.07936507936507936, 0.12698412698412698]
第5次迴圈時 i=5 x=3 cumulativeSum=11 i/n=0.5 sum=63 cumulativeSum/sum=0.1746031746031746 
[0.1, 0.2, 0.3, 0.4, 0.5]
[0.015873015873015872, 0.031746031746031744, 0.07936507936507936, 0.12698412698412698, 0.1746031746031746]
第6次迴圈時 i=6 x=7 cumulativeSum=18 i/n=0.6 sum=63 cumulativeSum/sum=0.2857142857142857 
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
[0.015873015873015872, 0.031746031746031744, 0.07936507936507936, 0.12698412698412698, 0.1746031746031746, 0.2857142857142857]
第7次迴圈時 i=7 x=9 cumulativeSum=27 i/n=0.7 sum=63 cumulativeSum/sum=0.42857142857142855 
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
[0.015873015873015872, 0.031746031746031744, 0.07936507936507936, 0.12698412698412698, 0.1746031746031746, 0.2857142857142857, 0.42857142857142855]
第8次迴圈時 i=8 x=10 cumulativeSum=37 i/n=0.8 sum=63 cumulativeSum/sum=0.5873015873015873 
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]
[0.015873015873015872, 0.031746031746031744, 0.07936507936507936, 0.12698412698412698, 0.1746031746031746, 0.2857142857142857, 0.42857142857142855, 0.5873015873015873]
第9次迴圈時 i=9 x=11 cumulativeSum=48 i/n=0.9 sum=63 cumulativeSum/sum=0.7619047619047619 
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
[0.015873015873015872, 0.031746031746031744, 0.07936507936507936, 0.12698412698412698, 0.1746031746031746, 0.2857142857142857, 0.42857142857142855, 0.5873015873015873, 0.7619047619047619]
第10次迴圈時 i=10 x=15 cumulativeSum=63 i/n=1.0 sum=63 cumulativeSum/sum=1.0 
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
[0.015873015873015872, 0.031746031746031744, 0.07936507936507936, 0.12698412698412698, 0.1746031746031746, 0.2857142857142857, 0.42857142857142855, 0.5873015873015873, 0.7619047619047619, 1.0]
羅倫茲曲線（Lorenz curve）
```
![](https://i.imgur.com/tDqGI6t.jpg)

```
import pandas as pd

data={
    "Duration":[50,40,None,None,90,20],
    "Pulse":[109,140,110,125,138,170]
}
df=pd.DataFrame(data)
print(df['Duration'].sum())

200.0
```
```
a=19
f'我今年{a}歲'

我今年19歲
```
```
price=10
amount=15
total=price*amount
f'你購買{amount}個,一個單價{price}元,總額{total}'

你購買15個,一個單價10元,總額150
```
#### 心得
```
要先了解羅倫茲曲線才能知道吉尼斯係數的用法
```
