# 分析資料
```python
import psycopg2
import pandas as pd
conn = psycopg2.connect(database="mydb", user="test01",
password="testpw", host="localhost",
port="5432")
print(conn.closed)
## 允許python執行postgresql語法
with conn.cursor() as cur:
## 觀察查詢筆數
print(cur.rowcount)
## SQL語法
sql = "select * from public.salary_max"
## 執行sql語法
cur.execute(sql)
## 取得欄位名稱
name = [desc[0] for desc in cur.description]
## fetchall是將所有收尋的資料都寫入 rows 中
# rows = pd.DataFrame(cur.fetchall())
rows = pd.DataFrame(cur.fetchall(),columns=name)
## 取出一筆資料
# rows = pd.DataFrame(cur.fetchone())
## 自行設定要幾筆資料
# rows = pd.DataFrame(cur.fetchmany(10))
## 觀察查詢筆數
print(cur.rowcount)
print(rows.shape)
rows.head()
```
0
-1
800
(800, 6)
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>check_n</th>
<th>id</th>
<th>date</th>
<th>clock_in</th>
<th>clock_out</th>
<th>salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>1</td>
<td>3686</td>
<td>2021-06-26</td>
<td>07:22:30</td>
<td>22:03:39</td>
<td>64716969172</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3686</td>
<td>2021-06-10</td>
<td>06:27:32</td>
<td>17:43:58</td>
<td>9572746424</td>
</tr>
<tr>
<td>2</td>
<td>3</td>
<td>3686</td>
<td>2021-06-26</td>
<td>10:23:31</td>
<td>19:54:49</td>
<td>50535635106</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
<td>3686</td>
<td>2021-06-06</td>
<td>09:25:13</td>
<td>17:57:49</td>
<td>57455281008</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>3686</td>
<td>2021-06-20</td>
<td>10:17:18</td>
<td>22:21:48</td>
<td>83254690126</td>
</tr>
</tbody>
</table>
</div>
```python
import seaborn as sns
ax = sns.boxplot(x="salary", y="id", data=rows,orient="h")
ax
```
<matplotlib.axes._subplots.AxesSubplot at 0x7fad5864f790>

```python
import matplotlib.pyplot as plt
ax = sns.lineplot(x='date', y='salary', data=rows)
plt.xticks(rotation=90)
ax
```
<matplotlib.axes._subplots.AxesSubplot at 0x7fad598b8d10>

```python
def diff_times_in_seconds(t1, t2):
# caveat emptor - assumes t1 & t2 are python times, on the same day and t2 is after t1
h1, m1, s1 = t1.hour, t1.minute, t1.second
h2, m2, s2 = t2.hour, t2.minute, t2.second
t1_secs = s1 + 60 * (m1 + 60*h1)
t2_secs = s2 + 60 * (m2 + 60*h2)
return( t2_secs - t1_secs)/3600
```
```python
## 時間計算, 以小時為單位
diff_times_in_seconds(rows.clock_in[0],rows.clock_out[0])
```
14.685833333333333
```python
time = []
for n in range(len(rows)):
time.append(diff_times_in_seconds(rows.clock_in[n],rows.clock_out[n]))
time
```
[14.685833333333333,
11.273888888888889,
9.521666666666667,
8.543333333333333,
12.075,
13.412222222222223,
12.104166666666666,
11.917222222222222,
-10.634722222222223,
11.295833333333333,
11.406666666666666,
8.703055555555556,
10.949444444444444,
6.372777777777777,
-11.818055555555556,
14.944166666666666,
14.8675,
11.337777777777777,
15.385555555555555,
17.496388888888887,
-11.415833333333333,
17.024444444444445,
11.016666666666667,
12.98,
11.93388888888889,
18.089444444444446,
22.374444444444446,
11.3225,
12.765833333333333,
13.960555555555555,
7.5072222222222225,
10.051666666666666,
12.272222222222222,
-12.580277777777777,
9.671111111111111,
5.010277777777778,
5.325277777777778,
12.049166666666666,
21.58888888888889,
-9.086944444444445,
5.045277777777778,
-9.291944444444445,
14.453055555555556,
12.806944444444444,
8.488888888888889,
-11.151388888888889,
12.859166666666667,
14.856944444444444,
7.265,
10.9725,
14.345,
14.516666666666667,
11.054444444444444,
15.639166666666666,
11.19,
14.042777777777777,
14.198611111111111,
14.963333333333333,
16.136388888888888,
12.465833333333334,
8.749444444444444,
7.360833333333333,
9.155555555555555,
13.191666666666666,
8.99638888888889,
7.051666666666667,
-12.640277777777778,
15.372777777777777,
8.752777777777778,
10.678611111111111,
-8.126944444444444,
15.08388888888889,
16.038333333333334,
-10.9675,
-6.867222222222222,
8.241388888888888,
4.825277777777778,
-12.28888888888889,
7.575833333333334,
15.691666666666666,
6.078055555555555,
12.875555555555556,
11.471111111111112,
18.23138888888889,
7.412222222222222,
11.359722222222222,
-11.888333333333334,
12.123611111111112,
8.264722222222222,
9.126388888888888,
9.735833333333334,
10.508611111111112,
8.908611111111112,
11.931944444444444,
-12.699166666666667,
6.0055555555555555,
6.3405555555555555,
9.919444444444444,
8.382777777777777,
8.219166666666666,
-9.619166666666667,
12.676388888888889,
-10.530833333333334,
6.001111111111111,
18.458333333333332,
16.0075,
9.115277777777777,
9.074166666666667,
12.964166666666667,
9.123055555555556,
16.362222222222222,
15.711944444444445,
9.094444444444445,
6.46,
18.761666666666667,
11.597222222222221,
10.977777777777778,
-12.901666666666667,
12.505,
14.869166666666667,
13.956666666666667,
-12.621944444444445,
12.088333333333333,
11.217777777777778,
15.796666666666667,
16.488333333333333,
9.605833333333333,
9.937777777777777,
13.764166666666666,
7.2572222222222225,
5.808055555555556,
-10.764722222222222,
-9.375555555555556,
18.08888888888889,
-10.714444444444444,
11.285277777777777,
11.18388888888889,
9.52361111111111,
10.777222222222223,
12.62888888888889,
-6.062222222222222,
10.114722222222222,
13.335,
9.316666666666666,
13.32611111111111,
-7.695277777777778,
16.380277777777778,
8.221944444444444,
6.043611111111111,
15.259722222222223,
12.991944444444444,
18.658055555555556,
14.413333333333334,
13.324444444444444,
-9.942777777777778,
8.487777777777778,
8.5325,
10.987222222222222,
17.52611111111111,
12.35,
7.473333333333334,
-6.228611111111111,
7.072222222222222,
9.51361111111111,
15.686388888888889,
11.08361111111111,
6.915,
-12.979444444444445,
9.995555555555555,
10.931111111111111,
-6.702222222222222,
12.841111111111111,
13.936666666666667,
-7.095277777777778,
15.939166666666667,
10.41638888888889,
-12.599722222222223,
-5.683888888888889,
9.5275,
16.586666666666666,
15.693055555555556,
6.990555555555556,
14.6775,
11.618611111111111,
13.788611111111111,
6.261666666666667,
14.3675,
13.800277777777778,
16.365555555555556,
-5.248611111111111,
12.246944444444445,
14.534166666666666,
13.988611111111112,
13.331388888888888,
10.400277777777777,
10.117777777777778,
14.3975,
17.369444444444444,
-11.195555555555556,
11.364166666666666,
8.671944444444444,
11.074166666666667,
-12.59388888888889,
10.14,
13.993333333333334,
-10.526111111111112,
11.305277777777778,
-10.1275,
13.509722222222223,
-11.618055555555555,
6.852222222222222,
-5.852222222222222,
12.381944444444445,
12.294722222222223,
-9.514166666666666,
-7.46,
-7.887777777777778,
6.560555555555555,
-6.976666666666667,
4.8661111111111115,
-8.398333333333333,
7.938888888888889,
-12.206944444444444,
-6.687777777777778,
11.723055555555556,
8.977222222222222,
9.120833333333334,
10.921944444444444,
20.958055555555557,
12.375277777777777,
12.25361111111111,
12.693055555555556,
13.450555555555555,
11.207777777777778,
7.3052777777777775,
-11.92361111111111,
15.56638888888889,
11.935277777777777,
11.852222222222222,
10.751944444444444,
-12.712777777777777,
12.738611111111112,
18.13277777777778,
6.896388888888889,
12.346666666666666,
9.811666666666667,
13.363055555555556,
11.45,
12.873888888888889,
-8.896944444444445,
13.029722222222222,
6.303333333333334,
13.065277777777778,
10.876111111111111,
12.6825,
10.726666666666667,
-11.841666666666667,
9.600555555555555,
-9.32611111111111,
11.968055555555555,
12.561666666666667,
7.926666666666667,
7.8825,
14.831666666666667,
10.901666666666667,
9.167222222222222,
10.326666666666666,
10.308055555555555,
0.0,
14.935833333333333,
14.292222222222222,
13.166944444444445,
17.863611111111112,
16.23,
11.130277777777778,
17.09722222222222,
-8.040833333333333,
8.364444444444445,
8.827222222222222,
13.62861111111111,
14.169166666666667,
14.815555555555555,
-7.294444444444444,
14.45861111111111,
11.078333333333333,
-7.819166666666667,
16.06972222222222,
8.803333333333333,
9.551666666666666,
7.004166666666666,
14.181111111111111,
13.260833333333334,
10.355555555555556,
13.71111111111111,
8.804166666666667,
11.920555555555556,
6.651944444444444,
10.543055555555556,
10.062222222222223,
9.594444444444445,
8.071111111111112,
14.274166666666666,
14.768888888888888,
7.201388888888889,
10.485,
18.469722222222224,
15.456111111111111,
11.391388888888889,
13.760833333333334,
-11.766944444444444,
8.614166666666666,
6.536111111111111,
6.904722222222222,
12.463611111111112,
6.19,
-9.556111111111111,
17.531666666666666,
9.704722222222221,
12.213333333333333,
10.028333333333334,
9.890277777777778,
14.985277777777778,
11.701666666666666,
16.453333333333333,
-7.905833333333334,
13.552777777777777,
12.697777777777778,
15.159166666666666,
12.401111111111112,
10.373611111111112,
6.771944444444444,
14.226944444444445,
-12.542777777777777,
12.09361111111111,
15.230277777777777,
14.405,
13.62888888888889,
17.7875,
5.867222222222222,
11.400833333333333,
9.26,
12.178333333333333,
-7.323611111111111,
8.525,
17.387777777777778,
13.171944444444444,
12.592222222222222,
13.625555555555556,
7.805,
12.374722222222223,
9.716666666666667,
6.882777777777778,
8.35,
9.33611111111111,
8.972222222222221,
-6.428333333333334,
11.886388888888888,
11.0475,
8.132777777777777,
10.076666666666666,
11.277222222222223,
17.183055555555555,
8.47,
7.535277777777778,
10.249444444444444,
4.76,
14.482222222222223,
8.917222222222222,
-5.8675,
0.0,
11.785277777777777,
18.2625,
8.908888888888889,
14.140555555555556,
9.210555555555555,
5.088611111111111,
14.069722222222222,
10.474722222222223,
10.894722222222223,
11.932222222222222,
11.68361111111111,
9.036944444444444,
11.654722222222222,
-7.406944444444444,
13.235833333333334,
17.345277777777778,
13.722777777777777,
19.386666666666667,
10.032777777777778,
-11.878333333333334,
8.701944444444445,
8.314722222222223,
12.774722222222222,
-12.105555555555556,
12.435833333333333,
16.581944444444446,
-9.364444444444445,
9.756388888888889,
17.835277777777776,
11.402777777777779,
23.196944444444444,
17.252777777777776,
10.728333333333333,
11.286666666666667,
-7.7316666666666665,
-10.624166666666667,
15.470555555555556,
8.041944444444445,
14.755,
9.163055555555555,
14.895277777777778,
8.904166666666667,
12.728055555555555,
13.970555555555556,
8.182777777777778,
13.793055555555556,
-12.976111111111111,
-6.509444444444444,
-12.185555555555556,
11.621666666666666,
13.782222222222222,
13.779166666666667,
6.911111111111111,
15.395277777777778,
6.8694444444444445,
9.1525,
8.780833333333334,
14.903888888888888,
10.091388888888888,
12.075,
13.73611111111111,
9.495555555555555,
-9.86,
13.149722222222222,
12.054444444444444,
16.19527777777778,
-7.491944444444444,
10.046666666666667,
14.246666666666666,
8.810833333333333,
13.275277777777777,
-7.238611111111111,
22.551111111111112,
6.463055555555556,
10.948611111111111,
8.790277777777778,
11.033611111111112,
14.5025,
10.600833333333334,
9.424444444444445,
14.497777777777777,
12.815833333333334,
13.119166666666667,
12.671111111111111,
10.502777777777778,
9.816666666666666,
16.981666666666666,
10.779722222222222,
-8.770833333333334,
13.2475,
10.806111111111111,
11.559444444444445,
12.205555555555556,
-5.925833333333333,
17.86111111111111,
13.66611111111111,
12.09361111111111,
12.771944444444445,
12.230277777777777,
11.74388888888889,
12.23,
6.581944444444445,
6.663333333333333,
11.354444444444445,
13.673333333333334,
15.29888888888889,
11.27111111111111,
9.044722222222223,
11.606944444444444,
8.485555555555555,
0.0,
8.695,
5.763333333333334,
12.43,
8.665555555555555,
14.799166666666666,
7.839722222222222,
5.8625,
9.7425,
8.70111111111111,
15.549722222222222,
-11.2175,
0.0,
5.603888888888889,
10.64861111111111,
12.8125,
-5.465833333333333,
10.5225,
14.741388888888888,
17.0725,
-5.901111111111111,
13.895,
10.727777777777778,
15.057222222222222,
19.04138888888889,
12.365555555555556,
16.135277777777777,
12.703611111111112,
9.03111111111111,
15.126111111111111,
9.750833333333333,
12.131666666666666,
-9.2975,
-12.378055555555555,
15.866666666666667,
14.416944444444445,
-11.445277777777777,
10.506944444444445,
10.474444444444444,
12.092777777777778,
7.168888888888889,
7.135277777777778,
14.171111111111111,
13.030277777777778,
20.1425,
11.12888888888889,
12.1275,
9.020555555555555,
16.144166666666667,
9.915555555555555,
18.720277777777778,
11.756388888888889,
11.299166666666666,
-11.386111111111111,
9.34361111111111,
23.219166666666666,
6.8116666666666665,
-12.926944444444445,
10.896944444444445,
11.078333333333333,
8.164444444444445,
6.736111111111111,
9.765,
21.655555555555555,
9.69138888888889,
12.968333333333334,
-10.51,
13.385277777777778,
9.701944444444445,
-11.959444444444445,
11.590277777777779,
11.376111111111111,
-6.182777777777778,
9.334722222222222,
10.062777777777777,
-11.356666666666667,
-9.696388888888889,
-7.179444444444444,
11.290833333333333,
13.009166666666667,
12.85361111111111,
-6.564444444444445,
8.329166666666667,
15.324166666666667,
7.823888888888889,
11.2575,
-9.344444444444445,
9.100277777777778,
-7.288333333333333,
11.341111111111111,
18.04388888888889,
6.651388888888889,
11.45861111111111,
13.921666666666667,
7.988888888888889,
11.247777777777777,
9.864722222222222,
10.31861111111111,
10.140277777777778,
-8.709722222222222,
-9.06861111111111,
14.395833333333334,
-12.82611111111111,
11.408055555555556,
6.628888888888889,
11.810277777777777,
-9.853333333333333,
13.060277777777777,
5.972222222222222,
10.043333333333333,
6.808611111111111,
8.368611111111111,
11.236666666666666,
13.602777777777778,
15.566666666666666,
12.651111111111112,
-7.161944444444444,
-8.750277777777777,
7.67,
12.793611111111112,
-9.840277777777779,
14.4,
14.743611111111111,
17.52166666666667,
8.360277777777778,
10.990555555555556,
13.52111111111111,
-6.769166666666667,
9.306666666666667,
10.705555555555556,
-10.063611111111111,
11.194166666666666,
-9.48,
-7.155,
9.875,
13.037222222222223,
12.677222222222222,
12.490277777777777,
-11.44361111111111,
6.29,
6.940555555555555,
11.308333333333334,
13.860277777777778,
14.942777777777778,
13.2775,
8.235555555555555,
0.0,
12.415277777777778,
15.689444444444444,
11.042222222222222,
13.539444444444445,
7.982222222222222,
10.554722222222223,
8.14861111111111,
12.178055555555556,
11.573611111111111,
5.683888888888889,
8.37,
5.381388888888889,
7.541388888888889,
11.385555555555555,
23.22,
9.665833333333333,
0.0,
22.55388888888889,
17.68777777777778,
9.995,
11.916944444444445,
9.834444444444445,
10.004166666666666,
12.018333333333333,
12.431944444444444,
17.46527777777778,
-7.4975,
18.140555555555554,
13.65,
12.805555555555555,
11.623055555555556,
16.463333333333335,
9.864166666666666,
-12.093333333333334,
5.3694444444444445,
10.258611111111112,
10.963888888888889,
23.088333333333335,
8.430277777777778,
6.790277777777778,
14.194722222222222,
8.23611111111111,
5.3341666666666665,
13.866666666666667,
13.740277777777777,
10.713333333333333,
6.861666666666666,
11.675833333333333,
8.846666666666666,
13.196388888888889,
12.179444444444444,
6.434722222222222,
9.793333333333333,
12.493055555555555,
23.974444444444444,
-12.399166666666666,
8.2125,
14.933333333333334,
-5.111666666666666,
-8.890833333333333,
13.985,
7.776111111111111,
10.942222222222222,
10.7725,
-9.607777777777779,
11.504166666666666,
12.855277777777777,
5.607222222222222,
12.94361111111111,
12.483333333333333,
14.304444444444444,
11.705277777777777,
8.163611111111111,
8.284722222222221,
17.635833333333334,
10.141666666666667,
-11.87111111111111,
12.548055555555555,
8.254444444444445,
5.923055555555556,
16.60527777777778,
23.194444444444443,
-8.773888888888889,
7.764166666666667,
8.81611111111111,
12.3875,
13.240833333333333,
8.916666666666666,
12.347222222222221,
6.091666666666667,
-11.491388888888888,
11.785277777777777,
15.096944444444444,
8.2375,
14.373333333333333,
6.836388888888889,
5.350555555555555,
10.63111111111111,
-10.276388888888889,
-5.364166666666667,
10.196944444444444,
9.735,
11.977777777777778,
8.669444444444444,
13.231111111111112,
14.214722222222223,
14.597222222222221,
-8.626944444444444,
-9.146666666666667,
12.875555555555556,
12.963333333333333,
14.268888888888888,
-5.573333333333333,
15.558333333333334,
12.589722222222223,
8.921111111111111,
11.885555555555555,
12.403055555555556,
11.135555555555555,
6.600833333333333,
14.400833333333333,
18.176111111111112,
10.12861111111111,
10.952777777777778,
13.622222222222222,
11.874444444444444,
-11.854166666666666,
9.8975,
9.181666666666667,
11.818333333333333,
15.358333333333333,
10.195,
4.695277777777778,
21.51638888888889,
8.099722222222223,
14.635,
8.672222222222222,
-5.976388888888889,
11.780833333333334,
16.02111111111111,
19.779444444444444,
10.63888888888889,
12.399722222222222,
9.054444444444444,
8.297222222222222,
-12.65638888888889,
8.29861111111111,
13.212777777777777,
9.099444444444444,
9.085833333333333,
12.522777777777778,
-8.76361111111111,
10.44138888888889,
11.450833333333334,
8.86888888888889,
8.930277777777778,
11.164444444444445,
11.280277777777778,
7.909166666666667,
8.622777777777777,
13.290833333333333,
13.3525,
-7.217222222222222,
11.765277777777778,
9.736666666666666,
10.56,
15.9625,
13.266666666666667,
-10.174166666666666,
16.925,
10.731388888888889,
13.188611111111111,
10.9175]
```python
rows.iloc[8]
```
check_n 9
id 3686
date 2021-06-07
clock_in 10:38:05
clock_out 00:00:00
salary 79893629039
Name: 8, dtype: object
```python
rows1 = []
for n in range(len(rows)):
if rows.clock_in[n].hour != 0 and rows.clock_out[n].hour != 0:
rows1.append(rows.iloc[n])
rows1 = pd.DataFrame(rows1,index=range(len(rows1)))
rows1.head()
```
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>check_n</th>
<th>id</th>
<th>date</th>
<th>clock_in</th>
<th>clock_out</th>
<th>salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>1</td>
<td>3686</td>
<td>2021-06-26</td>
<td>07:22:30</td>
<td>22:03:39</td>
<td>64716969172</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3686</td>
<td>2021-06-10</td>
<td>06:27:32</td>
<td>17:43:58</td>
<td>9572746424</td>
</tr>
<tr>
<td>2</td>
<td>3</td>
<td>3686</td>
<td>2021-06-26</td>
<td>10:23:31</td>
<td>19:54:49</td>
<td>50535635106</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
<td>3686</td>
<td>2021-06-06</td>
<td>09:25:13</td>
<td>17:57:49</td>
<td>57455281008</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>3686</td>
<td>2021-06-20</td>
<td>10:17:18</td>
<td>22:21:48</td>
<td>83254690126</td>
</tr>
</tbody>
</table>
</div>
```python
time = []
for n in range(len(rows1)):
time.append(diff_times_in_seconds(rows1.clock_in[n],rows1.clock_out[n]))
time
```
[14.685833333333333,
11.273888888888889,
9.521666666666667,
8.543333333333333,
12.075,
13.412222222222223,
12.104166666666666,
11.917222222222222,
11.295833333333333,
11.406666666666666,
8.703055555555556,
10.949444444444444,
6.372777777777777,
14.944166666666666,
14.8675,
11.337777777777777,
15.385555555555555,
17.496388888888887,
17.024444444444445,
11.016666666666667,
12.98,
11.93388888888889,
18.089444444444446,
11.3225,
12.765833333333333,
13.960555555555555,
7.5072222222222225,
10.051666666666666,
12.272222222222222,
9.671111111111111,
5.010277777777778,
5.325277777777778,
12.049166666666666,
5.045277777777778,
14.453055555555556,
12.806944444444444,
8.488888888888889,
12.859166666666667,
14.856944444444444,
7.265,
10.9725,
14.345,
14.516666666666667,
11.054444444444444,
15.639166666666666,
11.19,
14.042777777777777,
14.198611111111111,
14.963333333333333,
16.136388888888888,
12.465833333333334,
8.749444444444444,
7.360833333333333,
9.155555555555555,
13.191666666666666,
8.99638888888889,
7.051666666666667,
15.372777777777777,
8.752777777777778,
10.678611111111111,
15.08388888888889,
16.038333333333334,
8.241388888888888,
4.825277777777778,
7.575833333333334,
15.691666666666666,
6.078055555555555,
12.875555555555556,
11.471111111111112,
18.23138888888889,
7.412222222222222,
11.359722222222222,
12.123611111111112,
8.264722222222222,
9.126388888888888,
9.735833333333334,
10.508611111111112,
8.908611111111112,
11.931944444444444,
6.0055555555555555,
6.3405555555555555,
9.919444444444444,
8.382777777777777,
8.219166666666666,
12.676388888888889,
6.001111111111111,
18.458333333333332,
16.0075,
9.115277777777777,
9.074166666666667,
12.964166666666667,
9.123055555555556,
16.362222222222222,
15.711944444444445,
9.094444444444445,
6.46,
11.597222222222221,
10.977777777777778,
12.505,
14.869166666666667,
13.956666666666667,
12.088333333333333,
11.217777777777778,
15.796666666666667,
16.488333333333333,
9.605833333333333,
9.937777777777777,
13.764166666666666,
7.2572222222222225,
5.808055555555556,
18.08888888888889,
11.285277777777777,
11.18388888888889,
9.52361111111111,
10.777222222222223,
12.62888888888889,
10.114722222222222,
13.335,
9.316666666666666,
13.32611111111111,
16.380277777777778,
8.221944444444444,
6.043611111111111,
15.259722222222223,
12.991944444444444,
18.658055555555556,
14.413333333333334,
13.324444444444444,
8.487777777777778,
8.5325,
10.987222222222222,
17.52611111111111,
12.35,
7.473333333333334,
7.072222222222222,
9.51361111111111,
15.686388888888889,
11.08361111111111,
6.915,
9.995555555555555,
10.931111111111111,
12.841111111111111,
13.936666666666667,
15.939166666666667,
10.41638888888889,
9.5275,
16.586666666666666,
15.693055555555556,
6.990555555555556,
14.6775,
11.618611111111111,
13.788611111111111,
6.261666666666667,
14.3675,
13.800277777777778,
16.365555555555556,
12.246944444444445,
14.534166666666666,
13.988611111111112,
13.331388888888888,
10.400277777777777,
10.117777777777778,
14.3975,
17.369444444444444,
11.364166666666666,
8.671944444444444,
11.074166666666667,
10.14,
13.993333333333334,
11.305277777777778,
13.509722222222223,
6.852222222222222,
12.381944444444445,
12.294722222222223,
6.560555555555555,
4.8661111111111115,
7.938888888888889,
11.723055555555556,
8.977222222222222,
9.120833333333334,
10.921944444444444,
12.375277777777777,
12.25361111111111,
12.693055555555556,
13.450555555555555,
11.207777777777778,
7.3052777777777775,
15.56638888888889,
11.935277777777777,
11.852222222222222,
10.751944444444444,
12.738611111111112,
18.13277777777778,
6.896388888888889,
12.346666666666666,
9.811666666666667,
13.363055555555556,
11.45,
12.873888888888889,
13.029722222222222,
6.303333333333334,
13.065277777777778,
10.876111111111111,
12.6825,
10.726666666666667,
9.600555555555555,
11.968055555555555,
12.561666666666667,
7.926666666666667,
7.8825,
14.831666666666667,
10.901666666666667,
9.167222222222222,
10.326666666666666,
10.308055555555555,
14.935833333333333,
14.292222222222222,
13.166944444444445,
17.863611111111112,
16.23,
11.130277777777778,
17.09722222222222,
8.364444444444445,
8.827222222222222,
13.62861111111111,
14.169166666666667,
14.815555555555555,
14.45861111111111,
11.078333333333333,
16.06972222222222,
8.803333333333333,
9.551666666666666,
7.004166666666666,
14.181111111111111,
13.260833333333334,
10.355555555555556,
13.71111111111111,
8.804166666666667,
11.920555555555556,
6.651944444444444,
10.543055555555556,
10.062222222222223,
9.594444444444445,
8.071111111111112,
14.274166666666666,
14.768888888888888,
7.201388888888889,
10.485,
15.456111111111111,
11.391388888888889,
13.760833333333334,
8.614166666666666,
6.536111111111111,
6.904722222222222,
12.463611111111112,
6.19,
17.531666666666666,
9.704722222222221,
12.213333333333333,
10.028333333333334,
9.890277777777778,
14.985277777777778,
11.701666666666666,
16.453333333333333,
13.552777777777777,
12.697777777777778,
15.159166666666666,
12.401111111111112,
10.373611111111112,
6.771944444444444,
14.226944444444445,
12.09361111111111,
15.230277777777777,
14.405,
13.62888888888889,
17.7875,
5.867222222222222,
11.400833333333333,
9.26,
12.178333333333333,
8.525,
17.387777777777778,
13.171944444444444,
12.592222222222222,
13.625555555555556,
7.805,
12.374722222222223,
9.716666666666667,
6.882777777777778,
8.35,
9.33611111111111,
8.972222222222221,
11.886388888888888,
11.0475,
8.132777777777777,
10.076666666666666,
11.277222222222223,
17.183055555555555,
8.47,
7.535277777777778,
10.249444444444444,
4.76,
14.482222222222223,
8.917222222222222,
11.785277777777777,
18.2625,
8.908888888888889,
14.140555555555556,
9.210555555555555,
5.088611111111111,
14.069722222222222,
10.474722222222223,
10.894722222222223,
11.932222222222222,
11.68361111111111,
9.036944444444444,
11.654722222222222,
13.235833333333334,
13.722777777777777,
10.032777777777778,
8.701944444444445,
8.314722222222223,
12.774722222222222,
12.435833333333333,
16.581944444444446,
9.756388888888889,
17.835277777777776,
11.402777777777779,
17.252777777777776,
10.728333333333333,
11.286666666666667,
15.470555555555556,
8.041944444444445,
14.755,
9.163055555555555,
14.895277777777778,
8.904166666666667,
12.728055555555555,
13.970555555555556,
8.182777777777778,
13.793055555555556,
11.621666666666666,
13.782222222222222,
13.779166666666667,
6.911111111111111,
15.395277777777778,
6.8694444444444445,
9.1525,
8.780833333333334,
14.903888888888888,
10.091388888888888,
12.075,
13.73611111111111,
9.495555555555555,
13.149722222222222,
12.054444444444444,
16.19527777777778,
10.046666666666667,
14.246666666666666,
8.810833333333333,
13.275277777777777,
6.463055555555556,
10.948611111111111,
8.790277777777778,
11.033611111111112,
14.5025,
10.600833333333334,
9.424444444444445,
14.497777777777777,
12.815833333333334,
13.119166666666667,
12.671111111111111,
10.502777777777778,
9.816666666666666,
16.981666666666666,
10.779722222222222,
13.2475,
10.806111111111111,
11.559444444444445,
12.205555555555556,
17.86111111111111,
13.66611111111111,
12.09361111111111,
12.771944444444445,
12.230277777777777,
11.74388888888889,
12.23,
6.581944444444445,
6.663333333333333,
11.354444444444445,
13.673333333333334,
15.29888888888889,
11.27111111111111,
9.044722222222223,
11.606944444444444,
8.485555555555555,
8.695,
5.763333333333334,
12.43,
8.665555555555555,
14.799166666666666,
7.839722222222222,
5.8625,
9.7425,
8.70111111111111,
15.549722222222222,
5.603888888888889,
10.64861111111111,
12.8125,
10.5225,
14.741388888888888,
17.0725,
13.895,
10.727777777777778,
15.057222222222222,
12.365555555555556,
16.135277777777777,
12.703611111111112,
9.03111111111111,
15.126111111111111,
9.750833333333333,
12.131666666666666,
15.866666666666667,
14.416944444444445,
10.506944444444445,
10.474444444444444,
12.092777777777778,
7.168888888888889,
7.135277777777778,
14.171111111111111,
13.030277777777778,
11.12888888888889,
12.1275,
9.020555555555555,
16.144166666666667,
9.915555555555555,
18.720277777777778,
11.756388888888889,
11.299166666666666,
9.34361111111111,
6.8116666666666665,
10.896944444444445,
11.078333333333333,
8.164444444444445,
6.736111111111111,
9.765,
9.69138888888889,
12.968333333333334,
13.385277777777778,
9.701944444444445,
11.590277777777779,
11.376111111111111,
9.334722222222222,
10.062777777777777,
11.290833333333333,
13.009166666666667,
12.85361111111111,
8.329166666666667,
15.324166666666667,
7.823888888888889,
11.2575,
9.100277777777778,
11.341111111111111,
6.651388888888889,
11.45861111111111,
13.921666666666667,
7.988888888888889,
11.247777777777777,
9.864722222222222,
10.31861111111111,
10.140277777777778,
14.395833333333334,
11.408055555555556,
6.628888888888889,
11.810277777777777,
13.060277777777777,
5.972222222222222,
10.043333333333333,
6.808611111111111,
8.368611111111111,
11.236666666666666,
13.602777777777778,
15.566666666666666,
12.651111111111112,
7.67,
12.793611111111112,
14.4,
14.743611111111111,
17.52166666666667,
8.360277777777778,
10.990555555555556,
13.52111111111111,
9.306666666666667,
10.705555555555556,
11.194166666666666,
9.875,
13.037222222222223,
12.677222222222222,
12.490277777777777,
6.29,
6.940555555555555,
11.308333333333334,
13.860277777777778,
14.942777777777778,
13.2775,
8.235555555555555,
12.415277777777778,
15.689444444444444,
11.042222222222222,
13.539444444444445,
7.982222222222222,
10.554722222222223,
8.14861111111111,
12.178055555555556,
11.573611111111111,
5.683888888888889,
8.37,
5.381388888888889,
7.541388888888889,
11.385555555555555,
9.665833333333333,
9.995,
11.916944444444445,
9.834444444444445,
10.004166666666666,
12.018333333333333,
12.431944444444444,
17.46527777777778,
18.140555555555554,
13.65,
12.805555555555555,
11.623055555555556,
16.463333333333335,
9.864166666666666,
5.3694444444444445,
10.258611111111112,
10.963888888888889,
8.430277777777778,
6.790277777777778,
14.194722222222222,
8.23611111111111,
5.3341666666666665,
13.866666666666667,
13.740277777777777,
10.713333333333333,
6.861666666666666,
11.675833333333333,
8.846666666666666,
13.196388888888889,
12.179444444444444,
6.434722222222222,
9.793333333333333,
12.493055555555555,
8.2125,
14.933333333333334,
13.985,
7.776111111111111,
10.942222222222222,
10.7725,
11.504166666666666,
12.855277777777777,
5.607222222222222,
12.94361111111111,
12.483333333333333,
14.304444444444444,
11.705277777777777,
8.163611111111111,
8.284722222222221,
17.635833333333334,
10.141666666666667,
12.548055555555555,
8.254444444444445,
5.923055555555556,
16.60527777777778,
7.764166666666667,
8.81611111111111,
12.3875,
13.240833333333333,
8.916666666666666,
12.347222222222221,
6.091666666666667,
11.785277777777777,
15.096944444444444,
8.2375,
14.373333333333333,
6.836388888888889,
5.350555555555555,
10.63111111111111,
10.196944444444444,
9.735,
11.977777777777778,
8.669444444444444,
13.231111111111112,
14.214722222222223,
14.597222222222221,
12.875555555555556,
12.963333333333333,
14.268888888888888,
15.558333333333334,
12.589722222222223,
8.921111111111111,
11.885555555555555,
12.403055555555556,
11.135555555555555,
6.600833333333333,
14.400833333333333,
18.176111111111112,
10.12861111111111,
10.952777777777778,
13.622222222222222,
11.874444444444444,
9.8975,
9.181666666666667,
11.818333333333333,
15.358333333333333,
10.195,
4.695277777777778,
8.099722222222223,
14.635,
8.672222222222222,
11.780833333333334,
16.02111111111111,
10.63888888888889,
12.399722222222222,
9.054444444444444,
8.297222222222222,
8.29861111111111,
13.212777777777777,
9.099444444444444,
9.085833333333333,
12.522777777777778,
10.44138888888889,
11.450833333333334,
8.86888888888889,
8.930277777777778,
11.164444444444445,
11.280277777777778,
7.909166666666667,
8.622777777777777,
13.290833333333333,
13.3525,
11.765277777777778,
9.736666666666666,
10.56,
15.9625,
13.266666666666667,
16.925,
10.731388888888889,
13.188611111111111,
10.9175]
```python
import seaborn as sns
ax = sns.boxplot(x="salary", y="id", data=rows1,orient="h")
ax
```
<matplotlib.axes._subplots.AxesSubplot at 0x7fad5920b0d0>

```python
## 統計 兩位同事是不是有薪水上的差異
from scipy import stats
s1 = rows1[rows1["id"]==1735].salary
s2 = rows1[rows1["id"]==2264].salary
stats.ttest_ind(s1, s2)
```
Ttest_indResult(statistic=-2.0488037201723244, pvalue=0.044461151307115525)
```python
statistic, pvalue = stats.ttest_ind(s1, s2)
pvalue
```
0.044461151307115525
```python
rows1.groupby("id").size()
```
id
1735 33
2264 35
3686 33
3719 34
4155 33
4290 26
4513 31
4952 29
5586 33
5833 32
5956 31
6023 30
6479 32
6680 36
6931 36
7399 34
7445 32
7845 34
8483 33
8771 33
dtype: int64
```python
## 取出每個人前10高的薪水
rows2 = rows1.sort_values(['salary'], ascending=False).groupby('id').head(26)
rows2.index = range(len(rows2))
rows2.head()
```
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>check_n</th>
<th>id</th>
<th>date</th>
<th>clock_in</th>
<th>clock_out</th>
<th>salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>762</td>
<td>8771</td>
<td>2021-06-23</td>
<td>10:28:29</td>
<td>18:34:28</td>
<td>99925359873</td>
</tr>
<tr>
<td>1</td>
<td>359</td>
<td>6680</td>
<td>2021-06-15</td>
<td>10:01:26</td>
<td>18:09:24</td>
<td>99893342943</td>
</tr>
<tr>
<td>2</td>
<td>284</td>
<td>2264</td>
<td>2021-06-20</td>
<td>05:28:11</td>
<td>19:55:42</td>
<td>99887519278</td>
</tr>
<tr>
<td>3</td>
<td>395</td>
<td>6479</td>
<td>2021-06-22</td>
<td>05:08:34</td>
<td>17:34:43</td>
<td>99768790025</td>
</tr>
<tr>
<td>4</td>
<td>737</td>
<td>7399</td>
<td>2021-06-24</td>
<td>06:26:22</td>
<td>19:18:54</td>
<td>99749262338</td>
</tr>
</tbody>
</table>
</div>
```python
time = []
for n in range(len(rows2)):
time.append(diff_times_in_seconds(rows2.clock_in[n],rows2.clock_out[n]))
rows2["time_different"] = time
rows2.head()
```
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>check_n</th>
<th>id</th>
<th>date</th>
<th>clock_in</th>
<th>clock_out</th>
<th>salary</th>
<th>time_different</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>762</td>
<td>8771</td>
<td>2021-06-23</td>
<td>10:28:29</td>
<td>18:34:28</td>
<td>99925359873</td>
<td>8.099722</td>
</tr>
<tr>
<td>1</td>
<td>359</td>
<td>6680</td>
<td>2021-06-15</td>
<td>10:01:26</td>
<td>18:09:24</td>
<td>99893342943</td>
<td>8.132778</td>
</tr>
<tr>
<td>2</td>
<td>284</td>
<td>2264</td>
<td>2021-06-20</td>
<td>05:28:11</td>
<td>19:55:42</td>
<td>99887519278</td>
<td>14.458611</td>
</tr>
<tr>
<td>3</td>
<td>395</td>
<td>6479</td>
<td>2021-06-22</td>
<td>05:08:34</td>
<td>17:34:43</td>
<td>99768790025</td>
<td>12.435833</td>
</tr>
<tr>
<td>4</td>
<td>737</td>
<td>7399</td>
<td>2021-06-24</td>
<td>06:26:22</td>
<td>19:18:54</td>
<td>99749262338</td>
<td>12.875556</td>
</tr>
</tbody>
</table>
</div>
```python
## 畫圖趨勢圖,是不是時間越久,薪水越高
sns.lineplot(x="time_different",y = "salary",data=rows2)
```
<matplotlib.axes._subplots.AxesSubplot at 0x7fad5b21d790>

```python
## 製作分類,超過12為類別1,小於等於12為類別0
rows2["class"] = rows2["time_different"]
rows2["class"][rows2["class"]<=12] = 0
rows2["class"][rows2["class"]>12] = 1
rows2["class"]
```
/Users/yenjungchiu/opt/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:4: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
after removing the cwd from sys.path.
/Users/yenjungchiu/opt/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:5: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
"""
0 0.0
1 0.0
2 1.0
3 1.0
4 1.0
...
515 1.0
516 0.0
517 0.0
518 0.0
519 1.0
Name: class, Length: 520, dtype: float64
```python
## 分類是不是大於12小時的人,和少於12小時的人,可以用薪水來分出差異
import numpy as np
from sklearn import tree
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
np.array(rows2[["id","salary"]]), np.array(rows2["class"]), test_size=0.2)
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X_train, y_train)
```
```python
from sklearn.metrics import accuracy_score
pre = clf.predict(X_test)
accuracy_score(y_test, pre)
```
0.5673076923076923
# 功課
## 一、兩兩統計,輸出 同事一、同事二、檢定p value
** 同事一和同事二的搭配要去重覆,例如 (A,B)和(B,A)只能保留一個
```python
```
## 二、取出每一位同事,排名倒數10個的薪水,計算平均值,使用bar plot呈現
```python
```
## 三、使用分類模型,分析6/15前後,薪水與id是否可以預測上班時間長短
```python
```