城市气候与海洋的关系研究
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['font.sans-serif'] = ['SimSun']
plt.rcParams['axes.unicode_minus'] = False
导入数据各个海滨城市数据
- os.listdir(‘data3’)
- filter() 过滤出所有的.csv文件,返回一个可迭代的Filter对象
- df.append()以行的方式追加,返回一个新的DataFrame
import os
for csv_file in filter(lambda filename: filename.endswith('.csv'),
os.listdir('data3')):
print(csv_file)
asti_150715.csv
asti_250715.csv
asti_270615.csv
bologna_150715.csv
bologna_250715.csv
bologna_270615.csv
cesena_150715.csv
cesena_250715.csv
cesena_270615.csv
faenza_150715.csv
faenza_250715.csv
faenza_270615.csv
ferrara_150715.csv
ferrara_250715.csv
ferrara_270615.csv
mantova_150715.csv
mantova_250715.csv
mantova_270615.csv
milano_150715.csv
milano_250715.csv
milano_270615.csv
piacenza_150715.csv
piacenza_250715.csv
piacenza_270615.csv
ravenna_150715.csv
ravenna_250715.csv
ravenna_270615.csv
torino_150715.csv
torino_250715.csv
torino_270615.csv
for csv_file in filter(lambda filename: filename.endswith('.csv'),
os.listdir('data3')):
df= pd.read_csv(f'data3/{csv_file}')
display(df.head())
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 28.05 | 66 | 1014 | Sky is Clear | 1436863176 | 2.57 | 42.501 | Asti | 2015-07-14 10:39:36 | 315 |
1 | 1 | 29.51 | 64 | 1014 | Sky is Clear | 1436866759 | 1.54 | 263.000 | Asti | 2015-07-14 11:39:19 | 315 |
2 | 2 | 30.39 | 58 | 1017 | Sky is Clear | 1436870510 | 2.60 | 100.000 | Asti | 2015-07-14 12:41:50 | 315 |
3 | 3 | 31.10 | 54 | 1017 | Sky is Clear | 1436874098 | 2.10 | 90.000 | Asti | 2015-07-14 13:41:38 | 315 |
4 | 4 | 33.23 | 45 | 1016 | few clouds | 1436877645 | 2.10 | 120.000 | Asti | 2015-07-14 14:40:45 | 315 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 28.79 | 54 | 1012 | Sky is Clear | 1437730850 | 3.10 | 70 | Asti | 2015-07-24 11:40:50 | 315 |
1 | 1 | 30.02 | 58 | 1012 | Sky is Clear | 1437734493 | 3.10 | 70 | Asti | 2015-07-24 12:41:33 | 315 |
2 | 2 | 30.79 | 49 | 1007 | light rain | 1437738045 | 2.06 | 24 | Asti | 2015-07-24 13:40:45 | 315 |
3 | 3 | 32.02 | 51 | 1011 | few clouds | 1437741579 | 4.10 | 10 | Asti | 2015-07-24 14:39:39 | 315 |
4 | 4 | 30.93 | 51 | 1011 | few clouds | 1437745188 | 2.60 | 120 | Asti | 2015-07-24 15:39:48 | 315 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 22.68 | 60 | 1018 | Sky is Clear | 1435390925 | 2.10 | 80.000 | Asti | 2015-06-27 09:42:05 | 315 |
1 | 1 | 24.05 | 60 | 1018 | Sky is Clear | 1435394243 | 2.60 | 50.000 | Asti | 2015-06-27 10:37:23 | 315 |
2 | 2 | 26.56 | 57 | 1018 | Sky is Clear | 1435399017 | 2.10 | 100.000 | Asti | 2015-06-27 11:56:57 | 315 |
3 | 3 | 27.20 | 57 | 1017 | Sky is Clear | 1435402420 | 2.10 | 70.000 | Asti | 2015-06-27 12:53:40 | 315 |
4 | 4 | 28.56 | 29 | 1017 | Sky is Clear | 1435406056 | 2.06 | 154.505 | Asti | 2015-06-27 13:54:16 | 315 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 29.98 | 57 | 1021.0 | Sky is Clear | 1436863101 | 0.51 | 90.0 | Bologna | 2015-07-14 10:38:21 | 71 |
1 | 1 | 30.26 | 51 | 1021.0 | moderate rain | 1436866691 | 1.03 | 157.0 | Bologna | 2015-07-14 11:38:11 | 71 |
2 | 2 | 32.36 | 46 | 1021.0 | sky is clear | 1436870392 | 2.06 | 67.0 | Bologna | 2015-07-14 12:39:52 | 71 |
3 | 3 | 31.16 | 47 | 1021.0 | moderate rain | 1436874000 | 2.06 | 90.0 | Bologna | 2015-07-14 13:40:00 | 71 |
4 | 4 | 33.48 | 44 | 1021.0 | sky is clear | 1436877549 | 2.06 | 135.0 | Bologna | 2015-07-14 14:39:09 | 71 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 30.61 | 62 | 1017 | Sky is Clear | 1437730764 | 2.06 | 315 | Bologna | 2015-07-24 11:39:24 | 71 |
1 | 1 | 32.47 | 58 | 1009 | light rain | 1437734406 | 1.03 | 69 | Bologna | 2015-07-24 12:40:06 | 71 |
2 | 2 | 33.64 | 54 | 1008 | light rain | 1437737961 | 2.06 | 28 | Bologna | 2015-07-24 13:39:21 | 71 |
3 | 3 | 33.82 | 51 | 1008 | moderate rain | 1437741489 | 2.06 | 74 | Bologna | 2015-07-24 14:38:09 | 71 |
4 | 4 | 31.87 | 52 | 1008 | light rain | 1437745126 | 5.14 | 201 | Bologna | 2015-07-24 15:38:46 | 71 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 25.26 | 49 | 1023 | Sky is Clear | 1435390801 | 1.54 | 225.0 | Bologna | 2015-06-27 09:40:01 | 71 |
1 | 1 | 26.52 | 57 | 1016 | Sky is Clear | 1435394204 | 0.51 | 256.0 | Bologna | 2015-06-27 10:36:44 | 71 |
2 | 2 | 28.49 | 53 | 1016 | Sky is Clear | 1435398653 | 0.51 | 325.0 | Bologna | 2015-06-27 11:50:53 | 71 |
3 | 3 | 30.00 | 42 | 1022 | light rain | 1435402084 | 1.54 | 22.0 | Bologna | 2015-06-27 12:48:04 | 71 |
4 | 4 | 29.86 | 44 | 1015 | light rain | 1435405722 | 2.06 | 28.0 | Bologna | 2015-06-27 13:48:42 | 71 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 29.15 | 83 | 1015 | moderate rain | 1436863101 | 3.62 | 94.001 | Cesena | 2015-07-14 10:38:21 | 14 |
1 | 1 | 29.37 | 74 | 1015 | moderate rain | 1436866691 | 3.60 | 20.000 | Cesena | 2015-07-14 11:38:11 | 14 |
2 | 2 | 29.51 | 78 | 1015 | moderate rain | 1436870392 | 3.60 | 70.000 | Cesena | 2015-07-14 12:39:52 | 14 |
3 | 3 | 29.88 | 70 | 1016 | moderate rain | 1436874000 | 4.60 | 60.000 | Cesena | 2015-07-14 13:40:00 | 14 |
4 | 4 | 30.12 | 70 | 1016 | moderate rain | 1436877549 | 4.10 | 70.000 | Cesena | 2015-07-14 14:39:09 | 14 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 32.35 | 62 | 1010 | moderate rain | 1437730764 | 1.0 | 60.0 | Cesena | 2015-07-24 11:39:24 | 14 |
1 | 1 | 32.40 | 75 | 1009 | moderate rain | 1437734406 | 3.6 | 40.0 | Cesena | 2015-07-24 12:40:06 | 14 |
2 | 2 | 32.24 | 75 | 1009 | moderate rain | 1437737961 | 3.6 | 70.0 | Cesena | 2015-07-24 13:39:21 | 14 |
3 | 3 | 32.26 | 79 | 1009 | moderate rain | 1437741489 | 5.1 | 70.0 | Cesena | 2015-07-24 14:38:09 | 14 |
4 | 4 | 32.81 | 70 | 1008 | moderate rain | 1437745125 | 3.6 | 60.0 | Cesena | 2015-07-24 15:38:45 | 14 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 23.34 | 82 | 1017 | very heavy rain | 1435387623 | 1.91 | 175.511 | Cesena | 2015-06-27 08:47:03 | 14 |
1 | 1 | 24.95 | 69 | 1018 | very heavy rain | 1435390801 | 2.01 | 159.500 | Cesena | 2015-06-27 09:40:01 | 14 |
2 | 2 | 25.67 | 73 | 1017 | very heavy rain | 1435394204 | 2.10 | 100.000 | Cesena | 2015-06-27 10:36:44 | 14 |
3 | 3 | 26.17 | 69 | 1017 | very heavy rain | 1435398652 | 3.10 | 120.000 | Cesena | 2015-06-27 11:50:52 | 14 |
4 | 4 | 27.07 | 61 | 1016 | very heavy rain | 1435402083 | 3.10 | 110.000 | Cesena | 2015-06-27 12:48:03 | 14 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 29.40 | 83 | 1015 | moderate rain | 1436863177 | 3.62 | 94.001 | Faenza | 2015-07-14 10:39:37 | 37 |
1 | 1 | 30.12 | 78 | 1015 | moderate rain | 1436866759 | 3.10 | 80.000 | Faenza | 2015-07-14 11:39:19 | 37 |
2 | 2 | 30.10 | 78 | 1015 | moderate rain | 1436870510 | 3.60 | 70.000 | Faenza | 2015-07-14 12:41:50 | 37 |
3 | 3 | 30.75 | 74 | 1015 | moderate rain | 1436874099 | 4.60 | 90.000 | Faenza | 2015-07-14 13:41:39 | 37 |
4 | 4 | 30.71 | 66 | 1015 | moderate rain | 1436877646 | 5.10 | 100.000 | Faenza | 2015-07-14 14:40:46 | 37 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 32.08 | 79 | 1010 | moderate rain | 1437730850 | 1.5 | 310.0 | Faenza | 2015-07-24 11:40:50 | 37 |
1 | 1 | 32.40 | 75 | 1009 | moderate rain | 1437734493 | 3.6 | 40.0 | Faenza | 2015-07-24 12:41:33 | 37 |
2 | 2 | 32.74 | 75 | 1009 | moderate rain | 1437738046 | 3.6 | 70.0 | Faenza | 2015-07-24 13:40:46 | 37 |
3 | 3 | 32.70 | 79 | 1009 | moderate rain | 1437741579 | 5.1 | 70.0 | Faenza | 2015-07-24 14:39:39 | 37 |
4 | 4 | 32.33 | 70 | 1008 | moderate rain | 1437745188 | 3.6 | 60.0 | Faenza | 2015-07-24 15:39:48 | 37 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 25.44 | 69 | 1018 | very heavy rain | 1435390925 | 1.29 | 14.5002 | Faenza | 2015-06-27 09:42:05 | 37 |
1 | 1 | 26.38 | 73 | 1017 | very heavy rain | 1435394243 | 2.10 | 100.0000 | Faenza | 2015-06-27 10:37:23 | 37 |
2 | 2 | 27.70 | 69 | 1017 | very heavy rain | 1435399019 | 3.10 | 120.0000 | Faenza | 2015-06-27 11:56:59 | 37 |
3 | 3 | 29.04 | 61 | 1016 | very heavy rain | 1435402422 | 3.10 | 110.0000 | Faenza | 2015-06-27 12:53:42 | 37 |
4 | 4 | 29.11 | 69 | 1016 | very heavy rain | 1435406058 | 3.60 | 110.0000 | Faenza | 2015-06-27 13:54:18 | 37 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 30.44 | 60 | 1011.0 | moderate rain | 1436863096 | 1.03 | 180.0 | Ferrara | 2015-07-14 10:38:16 | 47 |
1 | 1 | 31.40 | 58 | 1011.0 | moderate rain | 1436866685 | 1.54 | 135.0 | Ferrara | 2015-07-14 11:38:05 | 47 |
2 | 2 | 31.95 | 54 | 1011.0 | moderate rain | 1436870387 | 0.51 | 113.0 | Ferrara | 2015-07-14 12:39:47 | 47 |
3 | 3 | 32.06 | 50 | 1011.0 | moderate rain | 1436873989 | 2.06 | 90.0 | Ferrara | 2015-07-14 13:39:49 | 47 |
4 | 4 | 32.63 | 49 | 1010.0 | moderate rain | 1436877535 | 1.54 | 68.0 | Ferrara | 2015-07-14 14:38:55 | 47 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 31.33 | 49 | 1006 | moderate rain | 1437730757 | 1.03 | 23.000 | Ferrara | 2015-07-24 11:39:17 | 47 |
1 | 1 | 32.91 | 45 | 1006 | moderate rain | 1437734399 | 1.54 | 45.000 | Ferrara | 2015-07-24 12:39:59 | 47 |
2 | 2 | 33.43 | 42 | 1005 | moderate rain | 1437737956 | 2.12 | 123.003 | Ferrara | 2015-07-24 13:39:16 | 47 |
3 | 3 | 33.43 | 42 | 1004 | moderate rain | 1437741482 | 2.12 | 123.003 | Ferrara | 2015-07-24 14:38:02 | 47 |
4 | 4 | 32.66 | 42 | 1004 | moderate rain | 1437745121 | 0.51 | 23.000 | Ferrara | 2015-07-24 15:38:41 | 47 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 23.13 | 74 | 1013 | very heavy rain | 1435387607 | 1.54 | 135.0000 | Ferrara | 2015-06-27 08:46:47 | 47 |
1 | 1 | 25.02 | 66 | 1013 | very heavy rain | 1435390790 | 0.51 | 45.0000 | Ferrara | 2015-06-27 09:39:50 | 47 |
2 | 2 | 26.79 | 58 | 1013 | very heavy rain | 1435394201 | 5.14 | 158.0000 | Ferrara | 2015-06-27 10:36:41 | 47 |
3 | 3 | 28.43 | 48 | 1012 | very heavy rain | 1435398622 | 1.29 | 14.5002 | Ferrara | 2015-06-27 11:50:22 | 47 |
4 | 4 | 29.44 | 43 | 1012 | very heavy rain | 1435402050 | 1.54 | 203.0000 | Ferrara | 2015-06-27 12:47:30 | 47 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 28.66 | 51 | 1016 | Sky is Clear | 1436863113 | 2.1 | 140 | Mantova | 2015-07-14 10:38:33 | 121 |
1 | 1 | 30.10 | 45 | 1016 | Sky is Clear | 1436866700 | 2.1 | 0 | Mantova | 2015-07-14 11:38:20 | 121 |
2 | 2 | 30.14 | 42 | 1016 | Sky is Clear | 1436870406 | 2.1 | 170 | Mantova | 2015-07-14 12:40:06 | 121 |
3 | 3 | 30.74 | 45 | 1016 | Sky is Clear | 1436874012 | 1.5 | 0 | Mantova | 2015-07-14 13:40:12 | 121 |
4 | 4 | 31.22 | 38 | 1015 | Sky is Clear | 1436877561 | 1.5 | 180 | Mantova | 2015-07-14 14:39:21 | 121 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 29.86 | 45 | 1011 | few clouds | 1437730773 | 1.0 | 0 | Mantova | 2015-07-24 11:39:33 | 121 |
1 | 1 | 31.14 | 48 | 1011 | few clouds | 1437734419 | 2.6 | 130 | Mantova | 2015-07-24 12:40:19 | 121 |
2 | 2 | 32.21 | 46 | 1010 | few clouds | 1437737969 | 2.6 | 130 | Mantova | 2015-07-24 13:39:29 | 121 |
3 | 3 | 33.62 | 41 | 1009 | few clouds | 1437741501 | 2.6 | 120 | Mantova | 2015-07-24 14:38:21 | 121 |
4 | 4 | 34.18 | 38 | 1009 | few clouds | 1437745133 | 3.6 | 130 | Mantova | 2015-07-24 15:38:53 | 121 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 23.40 | 60 | 1018 | Sky is Clear | 1435387660 | 1.5 | 70 | Mantova | 2015-06-27 08:47:40 | 121 |
1 | 1 | 25.60 | 54 | 1018 | Sky is Clear | 1435390815 | 1.5 | 110 | Mantova | 2015-06-27 09:40:15 | 121 |
2 | 2 | 27.23 | 39 | 1018 | Sky is Clear | 1435394208 | 1.5 | 170 | Mantova | 2015-06-27 10:36:48 | 121 |
3 | 3 | 28.11 | 32 | 1018 | Sky is Clear | 1435398705 | 1.5 | 210 | Mantova | 2015-06-27 11:51:45 | 121 |
4 | 4 | 28.88 | 35 | 1018 | Sky is Clear | 1435402130 | 1.5 | 180 | Mantova | 2015-06-27 12:48:50 | 121 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 28.57 | 54 | 1016 | Sky is Clear | 1436863175 | 2.1 | 100 | Milano | 2015-07-14 10:39:35 | 250 |
1 | 1 | 29.74 | 48 | 1016 | Sky is Clear | 1436866758 | 2.6 | 0 | Milano | 2015-07-14 11:39:18 | 250 |
2 | 2 | 31.12 | 48 | 1016 | Sky is Clear | 1436870509 | 2.6 | 140 | Milano | 2015-07-14 12:41:49 | 250 |
3 | 3 | 32.16 | 45 | 1015 | Sky is Clear | 1436874098 | 2.1 | 0 | Milano | 2015-07-14 13:41:38 | 250 |
4 | 4 | 33.59 | 43 | 1015 | Sky is Clear | 1436877644 | 3.1 | 80 | Milano | 2015-07-14 14:40:44 | 250 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 29.50 | 48 | 1011 | few clouds | 1437730849 | 3.6 | 90 | Milano | 2015-07-24 11:40:49 | 250 |
1 | 1 | 30.81 | 45 | 1011 | few clouds | 1437734492 | 2.6 | 70 | Milano | 2015-07-24 12:41:32 | 250 |
2 | 2 | 31.91 | 45 | 1010 | proximity shower rain | 1437738045 | 3.1 | 80 | Milano | 2015-07-24 13:40:45 | 250 |
3 | 3 | 32.72 | 40 | 1009 | proximity shower rain | 1437741578 | 3.6 | 130 | Milano | 2015-07-24 14:39:38 | 250 |
4 | 4 | 33.44 | 38 | 1009 | Sky is Clear | 1437745188 | 3.6 | 130 | Milano | 2015-07-24 15:39:48 | 250 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 24.69 | 60 | 1017 | Sky is Clear | 1435390925 | 2.6 | 140 | Milano | 2015-06-27 09:42:05 | 250 |
1 | 1 | 25.34 | 57 | 1017 | Sky is Clear | 1435394243 | 2.1 | 160 | Milano | 2015-06-27 10:37:23 | 250 |
2 | 2 | 27.70 | 51 | 1017 | Sky is Clear | 1435399015 | 1.5 | 210 | Milano | 2015-06-27 11:56:55 | 250 |
3 | 3 | 28.36 | 42 | 1017 | Sky is Clear | 1435402416 | 2.1 | 220 | Milano | 2015-06-27 12:53:36 | 250 |
4 | 4 | 29.45 | 42 | 1016 | few clouds | 1435406054 | 2.6 | 210 | Milano | 2015-06-27 13:54:14 | 250 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 27.99 | 54 | 1016 | Sky is Clear | 1436863096 | 2.10 | 100.0 | Piacenza | 2015-07-14 10:38:16 | 200 |
1 | 1 | 29.13 | 48 | 1016 | Sky is Clear | 1436866685 | 2.60 | 0.0 | Piacenza | 2015-07-14 11:38:05 | 200 |
2 | 2 | 30.21 | 48 | 1016 | Sky is Clear | 1436870387 | 2.60 | 140.0 | Piacenza | 2015-07-14 12:39:47 | 200 |
3 | 3 | 31.40 | 45 | 1015 | Sky is Clear | 1436873990 | 2.10 | 0.0 | Piacenza | 2015-07-14 13:39:50 | 200 |
4 | 4 | 31.88 | 48 | 1010 | Sky is Clear | 1436877535 | 0.51 | 23.0 | Piacenza | 2015-07-14 14:38:55 | 200 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 29.82 | 48 | 1011 | few clouds | 1437730757 | 3.60 | 90.0 | Piacenza | 2015-07-24 11:39:17 | 200 |
1 | 1 | 31.22 | 52 | 1006 | Sky is Clear | 1437734399 | 3.08 | 55.0 | Piacenza | 2015-07-24 12:39:59 | 200 |
2 | 2 | 32.56 | 45 | 1010 | Sky is Clear | 1437737956 | 3.10 | 80.0 | Piacenza | 2015-07-24 13:39:16 | 200 |
3 | 3 | 33.03 | 40 | 1009 | few clouds | 1437741482 | 3.60 | 130.0 | Piacenza | 2015-07-24 14:38:02 | 200 |
4 | 4 | 33.73 | 38 | 1009 | Sky is Clear | 1437745121 | 3.60 | 130.0 | Piacenza | 2015-07-24 15:38:41 | 200 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 22.57 | 67 | 1013 | Sky is Clear | 1435387607 | 1.03 | 279.0 | Piacenza | 2015-06-27 08:46:47 | 200 |
1 | 1 | 24.61 | 60 | 1017 | Sky is Clear | 1435390790 | 2.60 | 140.0 | Piacenza | 2015-06-27 09:39:50 | 200 |
2 | 2 | 25.48 | 57 | 1017 | Sky is Clear | 1435394201 | 2.10 | 160.0 | Piacenza | 2015-06-27 10:36:41 | 200 |
3 | 3 | 26.52 | 53 | 1012 | Sky is Clear | 1435398622 | 1.54 | 0.0 | Piacenza | 2015-06-27 11:50:22 | 200 |
4 | 4 | 28.00 | 49 | 1012 | sky is clear | 1435402050 | 1.03 | 230.0 | Piacenza | 2015-06-27 12:47:30 | 200 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 29.10 | 74 | 1015 | moderate rain | 1436863177 | 3.10 | 10.0 | Ravenna | 2015-07-14 10:39:37 | 8 |
1 | 1 | 29.51 | 74 | 1015 | moderate rain | 1436866759 | 3.60 | 20.0 | Ravenna | 2015-07-14 11:39:19 | 8 |
2 | 2 | 29.63 | 70 | 1016 | moderate rain | 1436870511 | 3.60 | 40.0 | Ravenna | 2015-07-14 12:41:51 | 8 |
3 | 3 | 30.17 | 37 | 1015 | moderate rain | 1436874106 | 4.63 | 90.0 | Ravenna | 2015-07-14 13:41:46 | 8 |
4 | 4 | 30.45 | 34 | 1015 | moderate rain | 1436877646 | 3.08 | 87.0 | Ravenna | 2015-07-14 14:40:46 | 8 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 32.18 | 54 | 1010 | moderate rain | 1437730851 | 2.11 | 330.003 | Ravenna | 2015-07-24 11:40:51 | 8 |
1 | 1 | 32.37 | 62 | 1010 | moderate rain | 1437734494 | 2.60 | 20.000 | Ravenna | 2015-07-24 12:41:34 | 8 |
2 | 2 | 32.79 | 75 | 1009 | moderate rain | 1437738046 | 3.60 | 70.000 | Ravenna | 2015-07-24 13:40:46 | 8 |
3 | 3 | 32.75 | 79 | 1009 | moderate rain | 1437741580 | 5.10 | 70.000 | Ravenna | 2015-07-24 14:39:40 | 8 |
4 | 4 | 32.72 | 70 | 1008 | moderate rain | 1437745188 | 3.10 | 10.000 | Ravenna | 2015-07-24 15:39:48 | 8 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 24.86 | 61 | 1017 | very heavy rain | 1435390925 | 2.01 | 159.5 | Ravenna | 2015-06-27 09:42:05 | 8 |
1 | 1 | 25.71 | 73 | 1017 | very heavy rain | 1435394244 | 2.10 | 100.0 | Ravenna | 2015-06-27 10:37:24 | 8 |
2 | 2 | 26.41 | 61 | 1017 | very heavy rain | 1435399021 | 4.60 | 80.0 | Ravenna | 2015-06-27 11:57:01 | 8 |
3 | 3 | 28.29 | 54 | 1016 | very heavy rain | 1435402423 | 4.60 | 90.0 | Ravenna | 2015-06-27 12:53:43 | 8 |
4 | 4 | 28.47 | 54 | 1016 | very heavy rain | 1435406060 | 6.20 | 80.0 | Ravenna | 2015-06-27 13:54:20 | 8 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 28.34 | 65 | 1017 | Sky is Clear | 1436863109 | 3.1 | 20 | Torino | 2015-07-14 10:38:29 | 357 |
1 | 1 | 29.25 | 65 | 1017 | Sky is Clear | 1436866696 | 3.1 | 80 | Torino | 2015-07-14 11:38:16 | 357 |
2 | 2 | 30.40 | 58 | 1017 | Sky is Clear | 1436870399 | 2.6 | 100 | Torino | 2015-07-14 12:39:59 | 357 |
3 | 3 | 31.37 | 54 | 1017 | Sky is Clear | 1436874005 | 2.1 | 90 | Torino | 2015-07-14 13:40:05 | 357 |
4 | 4 | 32.59 | 45 | 1016 | few clouds | 1436877558 | 2.1 | 120 | Torino | 2015-07-14 14:39:18 | 357 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 30.29 | 54 | 1012 | Sky is Clear | 1437730770 | 3.1 | 70 | Torino | 2015-07-24 11:39:30 | 357 |
1 | 1 | 30.68 | 58 | 1012 | Sky is Clear | 1437734413 | 3.1 | 70 | Torino | 2015-07-24 12:40:13 | 357 |
2 | 2 | 30.96 | 58 | 1012 | few clouds | 1437737966 | 3.6 | 70 | Torino | 2015-07-24 13:39:26 | 357 |
3 | 3 | 30.91 | 51 | 1011 | few clouds | 1437741497 | 4.1 | 10 | Torino | 2015-07-24 14:38:17 | 357 |
4 | 4 | 32.20 | 51 | 1011 | few clouds | 1437745130 | 2.6 | 120 | Torino | 2015-07-24 15:38:50 | 357 |
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 23.80 | 64 | 1019 | Sky is Clear | 1435387651 | 1.0 | 0 | Torino | 2015-06-27 08:47:31 | 357 |
1 | 1 | 24.68 | 60 | 1018 | Sky is Clear | 1435390804 | 2.1 | 80 | Torino | 2015-06-27 09:40:04 | 357 |
2 | 2 | 25.12 | 60 | 1018 | Sky is Clear | 1435394206 | 2.6 | 50 | Torino | 2015-06-27 10:36:46 | 357 |
3 | 3 | 25.54 | 60 | 1018 | Sky is Clear | 1435398681 | 2.6 | 50 | Torino | 2015-06-27 11:51:21 | 357 |
4 | 4 | 27.42 | 57 | 1017 | Sky is Clear | 1435402112 | 2.1 | 70 | Torino | 2015-06-27 12:48:32 | 357 |
full_df =None
for csv_file in filter(lambda filename: filename.endswith('.csv'),
os.listdir('data3')):
df= pd.read_csv(f'data3/{csv_file}')
if full_df is None:
full_df = df
else:
full_df = full_df.append(df)
full_df.head()
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 28.05 | 66 | 1014.0 | Sky is Clear | 1436863176 | 2.57 | 42.501 | Asti | 2015-07-14 10:39:36 | 315 |
1 | 1 | 29.51 | 64 | 1014.0 | Sky is Clear | 1436866759 | 1.54 | 263.000 | Asti | 2015-07-14 11:39:19 | 315 |
2 | 2 | 30.39 | 58 | 1017.0 | Sky is Clear | 1436870510 | 2.60 | 100.000 | Asti | 2015-07-14 12:41:50 | 315 |
3 | 3 | 31.10 | 54 | 1017.0 | Sky is Clear | 1436874098 | 2.10 | 90.000 | Asti | 2015-07-14 13:41:38 | 315 |
4 | 4 | 33.23 | 45 | 1016.0 | few clouds | 1436877645 | 2.10 | 120.000 | Asti | 2015-07-14 14:40:45 | 315 |
full_df.shape
(675, 11)
pd.read_csv('data3/asti_150715.csv')
Unnamed: 0 | temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 28.05 | 66 | 1014 | Sky is Clear | 1436863176 | 2.57 | 42.501 | Asti | 2015-07-14 10:39:36 | 315 |
1 | 1 | 29.51 | 64 | 1014 | Sky is Clear | 1436866759 | 1.54 | 263.000 | Asti | 2015-07-14 11:39:19 | 315 |
2 | 2 | 30.39 | 58 | 1017 | Sky is Clear | 1436870510 | 2.60 | 100.000 | Asti | 2015-07-14 12:41:50 | 315 |
3 | 3 | 31.10 | 54 | 1017 | Sky is Clear | 1436874098 | 2.10 | 90.000 | Asti | 2015-07-14 13:41:38 | 315 |
4 | 4 | 33.23 | 45 | 1016 | few clouds | 1436877645 | 2.10 | 120.000 | Asti | 2015-07-14 14:40:45 | 315 |
5 | 5 | 32.95 | 46 | 1016 | few clouds | 1436881329 | 2.10 | 110.000 | Asti | 2015-07-14 15:42:09 | 315 |
6 | 6 | 34.31 | 46 | 1015 | few clouds | 1436884929 | 2.10 | 100.000 | Asti | 2015-07-14 16:42:09 | 315 |
7 | 7 | 32.47 | 49 | 1015 | few clouds | 1436888513 | 2.10 | 100.000 | Asti | 2015-07-14 17:41:53 | 315 |
8 | 8 | 34.28 | 46 | 1015 | few clouds | 1436892132 | 1.50 | 100.000 | Asti | 2015-07-14 18:42:12 | 315 |
9 | 9 | 31.41 | 46 | 1014 | Sky is Clear | 1436895723 | 2.10 | 240.000 | Asti | 2015-07-14 19:42:03 | 315 |
10 | 10 | 29.72 | 58 | 1014 | Sky is Clear | 1436899336 | 2.10 | 70.000 | Asti | 2015-07-14 20:42:16 | 315 |
11 | 11 | 27.68 | 62 | 1015 | Sky is Clear | 1436902943 | 1.50 | 40.000 | Asti | 2015-07-14 21:42:23 | 315 |
12 | 12 | 26.44 | 69 | 1015 | Sky is Clear | 1436906532 | 1.50 | 30.000 | Asti | 2015-07-14 22:42:12 | 315 |
13 | 13 | 26.17 | 69 | 1016 | Sky is Clear | 1436910149 | 1.50 | 330.000 | Asti | 2015-07-14 23:42:29 | 315 |
14 | 14 | 25.21 | 78 | 1014 | Sky is Clear | 1436913739 | 0.51 | 0.000 | Asti | 2015-07-15 00:42:19 | 315 |
15 | 15 | 24.45 | 73 | 1016 | Sky is Clear | 1436917311 | 2.60 | 350.000 | Asti | 2015-07-15 01:41:51 | 315 |
16 | 16 | 24.32 | 73 | 1016 | Sky is Clear | 1436920931 | 2.60 | 340.000 | Asti | 2015-07-15 02:42:11 | 315 |
17 | 17 | 24.24 | 73 | 1016 | Sky is Clear | 1436924426 | 2.10 | 360.000 | Asti | 2015-07-15 03:40:26 | 315 |
18 | 18 | 24.00 | 73 | 1016 | Sky is Clear | 1436928078 | 1.50 | 310.000 | Asti | 2015-07-15 04:41:18 | 315 |
19 | 19 | 23.00 | 83 | 1017 | broken clouds | 1436931718 | 0.50 | 0.000 | Asti | 2015-07-15 05:41:58 | 315 |
20 | 20 | 23.00 | 78 | 1017 | scattered clouds | 1436935298 | 3.10 | 350.000 | Asti | 2015-07-15 06:41:38 | 315 |
21 | 21 | 25.00 | 73 | 1017 | few clouds | 1436938882 | 1.50 | 330.000 | Asti | 2015-07-15 07:41:22 | 315 |
22 | 22 | 27.00 | 65 | 1017 | few clouds | 1436942516 | 0.50 | 0.000 | Asti | 2015-07-15 08:41:56 | 315 |
23 | 23 | 27.00 | 65 | 1017 | Sky is Clear | 1436945951 | 2.10 | 50.000 | Asti | 2015-07-15 09:39:11 | 315 |
pd.read_csv('data3/asti_150715.csv').shape
(24, 11)
- 查看列数
full_df.columns
Index(['Unnamed: 0', 'temp', 'humidity', 'pressure', 'description', 'dt',
'wind_speed', 'wind_deg', 'city', 'day', 'dist'],
dtype='object')
- 去除没用的列 Unnamed:0
# del all_['Unnamed:0'] 删除一列
# 方法二
full_df.drop(columns='Unnamed: 0',inplace=True)
full_df.columns
Index(['temp', 'humidity', 'pressure', 'description', 'dt', 'wind_speed',
'wind_deg', 'city', 'day', 'dist'],
dtype='object')
显示最高温度与力海远近的关系
- 按city分组,计算temp和dist的最大值
- 按dist排序,df.sort_values()
full_df.head()
temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 28.05 | 66 | 1014.0 | Sky is Clear | 1436863176 | 2.57 | 42.501 | Asti | 2015-07-14 10:39:36 | 315 |
1 | 29.51 | 64 | 1014.0 | Sky is Clear | 1436866759 | 1.54 | 263.000 | Asti | 2015-07-14 11:39:19 | 315 |
2 | 30.39 | 58 | 1017.0 | Sky is Clear | 1436870510 | 2.60 | 100.000 | Asti | 2015-07-14 12:41:50 | 315 |
3 | 31.10 | 54 | 1017.0 | Sky is Clear | 1436874098 | 2.10 | 90.000 | Asti | 2015-07-14 13:41:38 | 315 |
4 | 33.23 | 45 | 1016.0 | few clouds | 1436877645 | 2.10 | 120.000 | Asti | 2015-07-14 14:40:45 | 315 |
full_df.dtypes
temp float64
humidity int64
pressure float64
description object
dt int64
wind_speed float64
wind_deg float64
city object
day object
dist int64
dtype: object
dist_temp = full_df.groupby('city')['temp','dist'].max().sort_values('dist')
dist_temp
temp | dist | |
---|---|---|
city | ||
Ravenna | 32.79 | 8 |
Cesena | 32.81 | 14 |
Faenza | 32.74 | 37 |
Ferrara | 33.43 | 47 |
Bologna | 33.85 | 71 |
Mantova | 34.18 | 121 |
Piacenza | 33.92 | 200 |
Milano | 34.81 | 250 |
Asti | 34.31 | 315 |
Torino | 34.69 | 357 |
画图显示关系
- scatter()显示距离与温度的散布图
- plot()显示距离与温度的线性图
plt.scatter(dist_temp.dist,dist_temp.temp,s=50,c='r')
plt.plot(dist_temp.dist,dist_temp.temp,ls=':',c='b')
plt.show()
读取Astil城市某一天的温度变化数据,并画出线型图和散点图,如2015-7-14这一天的天气变化
- 处理day时间格式
- 转成年、月、日的date日期
- 或转成时、分、秒的字符串
- 将绘制的画布保存成一张png图片
full_df.city.unique()
array(['Asti', 'Bologna', 'Cesena', 'Faenza', 'Ferrara', 'Mantova',
'Milano', 'Piacenza', 'Ravenna', 'Torino'], dtype=object)
asti = full_df.query('city=="Asti"')
asti.head()
temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 28.05 | 66 | 1014.0 | Sky is Clear | 1436863176 | 2.57 | 42.501 | Asti | 2015-07-14 10:39:36 | 315 |
1 | 29.51 | 64 | 1014.0 | Sky is Clear | 1436866759 | 1.54 | 263.000 | Asti | 2015-07-14 11:39:19 | 315 |
2 | 30.39 | 58 | 1017.0 | Sky is Clear | 1436870510 | 2.60 | 100.000 | Asti | 2015-07-14 12:41:50 | 315 |
3 | 31.10 | 54 | 1017.0 | Sky is Clear | 1436874098 | 2.10 | 90.000 | Asti | 2015-07-14 13:41:38 | 315 |
4 | 33.23 | 45 | 1016.0 | few clouds | 1436877645 | 2.10 | 120.000 | Asti | 2015-07-14 14:40:45 | 315 |
def covert_date_hour(item):
return Series({'date':item[:10],'hour':item[11:13]})
asti['day'].map(covert_date_hour)
0 date 2015-07-14
hour 10
dtype: o...
1 date 2015-07-14
hour 11
dtype: o...
2 date 2015-07-14
hour 12
dtype: o...
3 date 2015-07-14
hour 13
dtype: o...
4 date 2015-07-14
hour 14
dtype: o...
...
15 date 2015-06-28
hour 04
dtype: o...
16 date 2015-06-28
hour 05
dtype: o...
17 date 2015-06-28
hour 06
dtype: o...
18 date 2015-06-28
hour 07
dtype: o...
19 date 2015-06-28
hour 08
dtype: o...
Name: day, Length: 68, dtype: object
def covert_date(item):
return item[:10]
def covert_hour(item):
return item[11:13]
asti['day'].map(covert_date)
0 2015-07-14
1 2015-07-14
2 2015-07-14
3 2015-07-14
4 2015-07-14
...
15 2015-06-28
16 2015-06-28
17 2015-06-28
18 2015-06-28
19 2015-06-28
Name: day, Length: 68, dtype: object
# 添加date列,hour列
asti['date'] = asti['day'].map(covert_date)
asti['hour'] = asti['day'].map(covert_hour)
asti.head()
D:\yingyong\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
D:\yingyong\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
This is separate from the ipykernel package so we can avoid doing imports until
temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | date | hour | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 28.05 | 66 | 1014.0 | Sky is Clear | 1436863176 | 2.57 | 42.501 | Asti | 2015-07-14 10:39:36 | 315 | 2015-07-14 | 10 |
1 | 29.51 | 64 | 1014.0 | Sky is Clear | 1436866759 | 1.54 | 263.000 | Asti | 2015-07-14 11:39:19 | 315 | 2015-07-14 | 11 |
2 | 30.39 | 58 | 1017.0 | Sky is Clear | 1436870510 | 2.60 | 100.000 | Asti | 2015-07-14 12:41:50 | 315 | 2015-07-14 | 12 |
3 | 31.10 | 54 | 1017.0 | Sky is Clear | 1436874098 | 2.10 | 90.000 | Asti | 2015-07-14 13:41:38 | 315 | 2015-07-14 | 13 |
4 | 33.23 | 45 | 1016.0 | few clouds | 1436877645 | 2.10 | 120.000 | Asti | 2015-07-14 14:40:45 | 315 | 2015-07-14 | 14 |
asti.query('date == "2015-07-14"')[['hour','temp']]
hour | temp | |
---|---|---|
0 | 10 | 28.05 |
1 | 11 | 29.51 |
2 | 12 | 30.39 |
3 | 13 | 31.10 |
4 | 14 | 33.23 |
5 | 15 | 32.95 |
6 | 16 | 34.31 |
7 | 17 | 32.47 |
8 | 18 | 34.28 |
9 | 19 | 31.41 |
10 | 20 | 29.72 |
11 | 21 | 27.68 |
12 | 22 | 26.44 |
13 | 23 | 26.17 |
asti1 = asti.query('date == "2015-07-14"')[['hour','temp']]
plt.plot(asti1.hour,asti1.temp)
[<matplotlib.lines.Line2D at 0x9541270>]
查看asti的一周的湿度变化的数据,并画出散点与线型图
- 按天进行分组
- 将分组后的data设置为列
asti.head()
temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | date | hour | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 28.05 | 66 | 1014.0 | Sky is Clear | 1436863176 | 2.57 | 42.501 | Asti | 2015-07-14 10:39:36 | 315 | 2015-07-14 | 10 |
1 | 29.51 | 64 | 1014.0 | Sky is Clear | 1436866759 | 1.54 | 263.000 | Asti | 2015-07-14 11:39:19 | 315 | 2015-07-14 | 11 |
2 | 30.39 | 58 | 1017.0 | Sky is Clear | 1436870510 | 2.60 | 100.000 | Asti | 2015-07-14 12:41:50 | 315 | 2015-07-14 | 12 |
3 | 31.10 | 54 | 1017.0 | Sky is Clear | 1436874098 | 2.10 | 90.000 | Asti | 2015-07-14 13:41:38 | 315 | 2015-07-14 | 13 |
4 | 33.23 | 45 | 1016.0 | few clouds | 1436877645 | 2.10 | 120.000 | Asti | 2015-07-14 14:40:45 | 315 | 2015-07-14 | 14 |
asti.date.unique()
array(['2015-07-14', '2015-07-15', '2015-07-24', '2015-07-25',
'2015-06-27', '2015-06-28'], dtype=object)
asti.columns
Index(['temp', 'humidity', 'pressure', 'description', 'dt', 'wind_speed',
'wind_deg', 'city', 'day', 'dist', 'date', 'hour'],
dtype='object')
asti.groupby('date')['humidity'].mean()
date
2015-06-27 54.083333
2015-06-28 75.250000
2015-07-14 55.571429
2015-07-15 73.400000
2015-07-24 55.692308
2015-07-25 73.272727
Name: humidity, dtype: float64
asti.groupby('date')['humidity'].mean().reset_index(drop=False)
date | humidity | |
---|---|---|
0 | 2015-06-27 | 54.083333 |
1 | 2015-06-28 | 75.250000 |
2 | 2015-07-14 | 55.571429 |
3 | 2015-07-15 | 73.400000 |
4 | 2015-07-24 | 55.692308 |
5 | 2015-07-25 | 73.272727 |
asti2 = asti.groupby('date')['humidity'].mean().reset_index(drop=False)
asti2
date | humidity | |
---|---|---|
0 | 2015-06-27 | 54.083333 |
1 | 2015-06-28 | 75.250000 |
2 | 2015-07-14 | 55.571429 |
3 | 2015-07-15 | 73.400000 |
4 | 2015-07-24 | 55.692308 |
5 | 2015-07-25 | 73.272727 |
plt.scatter(asti2.date,asti2.humidity)
<matplotlib.collections.PathCollection at 0xabab650>
plt.plot(asti2.date,asti2.humidity)
[<matplotlib.lines.Line2D at 0xabab730>]
观察发现,离海近的可以形成一条直线,离海远的也能形成一条直线
分别以100公里和50公里为分界点,划分为离海近和离海远的两组数据
- city_temp[‘dist’] > 50 # 远距离
- city_temp[‘dist’] <100 # 近距离
dist_temp
temp | dist | |
---|---|---|
city | ||
Ravenna | 32.79 | 8 |
Cesena | 32.81 | 14 |
Faenza | 32.74 | 37 |
Ferrara | 33.43 | 47 |
Bologna | 33.85 | 71 |
Mantova | 34.18 | 121 |
Piacenza | 33.92 | 200 |
Milano | 34.81 | 250 |
Asti | 34.31 | 315 |
Torino | 34.69 | 357 |
dist_temp.query('dist >50')
temp | dist | |
---|---|---|
city | ||
Bologna | 33.85 | 71 |
Mantova | 34.18 | 121 |
Piacenza | 33.92 | 200 |
Milano | 34.81 | 250 |
Asti | 34.31 | 315 |
Torino | 34.69 | 357 |
dist_temp.query('dist < 100')
temp | dist | |
---|---|---|
city | ||
Ravenna | 32.79 | 8 |
Cesena | 32.81 | 14 |
Faenza | 32.74 | 37 |
Ferrara | 33.43 | 47 |
Bologna | 33.85 | 71 |
dt1 = dist_temp[dist_temp.dist > 50]
dt2 = dist_temp[dist_temp.dist < 100]
display(dt1,dt2)
temp | dist | |
---|---|---|
city | ||
Bologna | 33.85 | 71 |
Mantova | 34.18 | 121 |
Piacenza | 33.92 | 200 |
Milano | 34.81 | 250 |
Asti | 34.31 | 315 |
Torino | 34.69 | 357 |
temp | dist | |
---|---|---|
city | ||
Ravenna | 32.79 | 8 |
Cesena | 32.81 | 14 |
Faenza | 32.74 | 37 |
Ferrara | 33.43 | 47 |
Bologna | 33.85 | 71 |
- 画出远、近的距离与温度的散布图(散点图)
plt.scatter(dt1.dist,dt1.temp,c='r',s =50)
plt.scatter(dt2.dist,dt2.temp,c='b',s = 100)
plt.show()
使用线性回归预测给定距离的温度
- 机器学习的线性回归模型LinearRegression
- scikit-learn 模块中线性模型
- sklearn.linear_model
- 创建回归模型对象
- linear = LinearRegression()
- 训练模型
- fit(x,y)
- x 是实际数据的自变量,必须是二维数组
- y 实际数据的因变量
- 预测模型
- predict(x)根据预测函数,计算x的结果
- x也是一个二维数组
from sklearn.linear_model import LinearRegression
# 线性回归模型:有标签(目标)的机器学习,(有监督)
linear = LinearRegression()
# reshape(-1,1) 其中的-1表示为任意数量的行,1表示1列
linear.fit(dt1.dist.values.reshape(-1,1),dt1.temp) # 训练
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)
linear.intercept_ # b
33.733483439733746
linear.coef_ # w
array([0.00255639])
-
线性函数:y = w*x + b
-
创建线性回归模型对象
训练模型
- 一个被训练过的模型,就是一个预测函数表达式f(x)=wx+b
预测模型
# 生成预测数据
x1 = np.linspace(50,400,num=50) # 远距离的预测数据
x2 = np.linspace(0,100,num=50) # 近距离的预测数据
y1 = linear.predict(x1.reshape(-1,1))
linear = LinearRegression()
linear.fit(dt2.dist.values.reshape(-1,1),dt2.temp)
y2 = linear.predict(x2.reshape(-1,1))
plt.scatter(dt1.dist,dt1.temp,c='r',s =50)
plt.plot(x1,y1,color='r')
plt.scatter(dt2.dist,dt2.temp,c='b',s = 100)
plt.plot(x2,y2,color='b')
plt.show()
风向与风速的关系
- 查看Asti城市的风向wind_deg与风速wind_speed
- 按wind_deg风向排序
- 重新创建索引 reset_index()
asti
temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | date | hour | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 28.05 | 66 | 1014.0 | Sky is Clear | 1436863176 | 2.57 | 42.501 | Asti | 2015-07-14 10:39:36 | 315 | 2015-07-14 | 10 |
1 | 29.51 | 64 | 1014.0 | Sky is Clear | 1436866759 | 1.54 | 263.000 | Asti | 2015-07-14 11:39:19 | 315 | 2015-07-14 | 11 |
2 | 30.39 | 58 | 1017.0 | Sky is Clear | 1436870510 | 2.60 | 100.000 | Asti | 2015-07-14 12:41:50 | 315 | 2015-07-14 | 12 |
3 | 31.10 | 54 | 1017.0 | Sky is Clear | 1436874098 | 2.10 | 90.000 | Asti | 2015-07-14 13:41:38 | 315 | 2015-07-14 | 13 |
4 | 33.23 | 45 | 1016.0 | few clouds | 1436877645 | 2.10 | 120.000 | Asti | 2015-07-14 14:40:45 | 315 | 2015-07-14 | 14 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
15 | 18.44 | 77 | 1018.0 | few clouds | 1435460039 | 2.60 | 30.000 | Asti | 2015-06-28 04:53:59 | 315 | 2015-06-28 | 04 |
16 | 18.01 | 77 | 1018.0 | Sky is Clear | 1435463877 | 2.10 | 360.000 | Asti | 2015-06-28 05:57:57 | 315 | 2015-06-28 | 05 |
17 | 18.58 | 84 | 1016.0 | Sky is Clear | 1435467179 | 0.88 | 321.501 | Asti | 2015-06-28 06:52:59 | 315 | 2015-06-28 | 06 |
18 | 20.08 | 73 | 1018.0 | Sky is Clear | 1435470849 | 1.00 | 0.000 | Asti | 2015-06-28 07:54:09 | 315 | 2015-06-28 | 07 |
19 | 20.98 | 68 | 1018.0 | Sky is Clear | 1435474468 | 1.00 | 0.000 | Asti | 2015-06-28 08:54:28 | 315 | 2015-06-28 | 08 |
68 rows × 12 columns
asti_ds = asti[['wind_deg','wind_speed']]
asti_ds.sort_values('wind_deg',inplace=True)
asti_ds
D:\yingyong\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: 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
wind_deg | wind_speed | |
---|---|---|
19 | 0.0 | 1.0 |
19 | 0.0 | 0.5 |
5 | 0.0 | 2.1 |
7 | 0.0 | 1.0 |
8 | 0.0 | 1.0 |
... | ... | ... |
15 | 350.0 | 2.6 |
20 | 350.0 | 3.1 |
17 | 360.0 | 2.1 |
14 | 360.0 | 1.5 |
16 | 360.0 | 2.1 |
68 rows × 2 columns
asti_ds.reset_index(inplace=True)
asti_ds
index | wind_deg | wind_speed | |
---|---|---|---|
0 | 0 | 0.0 | 1.0 |
1 | 1 | 0.0 | 0.5 |
2 | 2 | 0.0 | 2.1 |
3 | 3 | 0.0 | 1.0 |
4 | 4 | 0.0 | 1.0 |
... | ... | ... | ... |
63 | 63 | 350.0 | 2.6 |
64 | 64 | 350.0 | 3.1 |
65 | 65 | 360.0 | 2.1 |
66 | 66 | 360.0 | 1.5 |
67 | 67 | 360.0 | 2.1 |
68 rows × 3 columns
asti_ds.drop(columns='index',inplace=True)
asti_ds
wind_deg | wind_speed | |
---|---|---|
0 | 0.0 | 1.0 |
1 | 0.0 | 0.5 |
2 | 0.0 | 2.1 |
3 | 0.0 | 1.0 |
4 | 0.0 | 1.0 |
... | ... | ... |
63 | 350.0 | 2.6 |
64 | 350.0 | 3.1 |
65 | 360.0 | 2.1 |
66 | 360.0 | 1.5 |
67 | 360.0 | 2.1 |
68 rows × 2 columns
画出风向与风俗的线性图
- 按wind_deg分组,并计算wind_speed的平均风速,最后再plot()画出线形图
asti_ds_mean = asti_ds.groupby('wind_deg').mean()
asti_ds_mean
wind_speed | |
---|---|
wind_deg | |
0.000 | 1.018667 |
10.000 | 3.600000 |
24.000 | 2.060000 |
30.000 | 2.050000 |
40.000 | 1.500000 |
42.501 | 2.570000 |
50.000 | 2.075000 |
70.000 | 2.600000 |
80.000 | 2.100000 |
90.000 | 1.800000 |
100.000 | 2.083333 |
110.000 | 2.100000 |
120.000 | 2.350000 |
140.000 | 1.500000 |
154.505 | 2.060000 |
170.000 | 5.700000 |
180.000 | 1.000000 |
240.000 | 2.100000 |
263.000 | 1.540000 |
270.000 | 2.600000 |
280.000 | 2.100000 |
290.000 | 1.500000 |
300.000 | 3.600000 |
310.000 | 1.500000 |
320.000 | 2.600000 |
321.501 | 0.880000 |
330.000 | 1.500000 |
340.000 | 2.600000 |
350.000 | 2.233333 |
360.000 | 1.900000 |
plt.plot(asti_ds_mean)
[<matplotlib.lines.Line2D at 0xc3b2170>]
在子图中,同时比较风向与湿度和风力的关系
- 查看humidity列的数据类型
- 通过pd.to_numeric()将字符类型转成数值型
asti_dsht = asti[['wind_deg','wind_speed','humidity','temp']]
asti_dsht
wind_deg | wind_speed | humidity | temp | |
---|---|---|---|---|
0 | 42.501 | 2.57 | 66 | 28.05 |
1 | 263.000 | 1.54 | 64 | 29.51 |
2 | 100.000 | 2.60 | 58 | 30.39 |
3 | 90.000 | 2.10 | 54 | 31.10 |
4 | 120.000 | 2.10 | 45 | 33.23 |
... | ... | ... | ... | ... |
15 | 30.000 | 2.60 | 77 | 18.44 |
16 | 360.000 | 2.10 | 77 | 18.01 |
17 | 321.501 | 0.88 | 84 | 18.58 |
18 | 0.000 | 1.00 | 73 | 20.08 |
19 | 0.000 | 1.00 | 68 | 20.98 |
68 rows × 4 columns
asti_dsht = asti_dsht.sort_values('wind_deg').reset_index(drop=True)
asti_dsht
wind_deg | wind_speed | humidity | temp | |
---|---|---|---|---|
0 | 0.0 | 1.0 | 68 | 20.98 |
1 | 0.0 | 0.5 | 83 | 23.00 |
2 | 0.0 | 2.1 | 48 | 29.12 |
3 | 0.0 | 1.0 | 54 | 29.04 |
4 | 0.0 | 1.0 | 65 | 27.89 |
... | ... | ... | ... | ... |
63 | 350.0 | 2.6 | 73 | 24.45 |
64 | 350.0 | 3.1 | 78 | 23.00 |
65 | 360.0 | 2.1 | 73 | 24.24 |
66 | 360.0 | 1.5 | 73 | 19.81 |
67 | 360.0 | 2.1 | 77 | 18.01 |
68 rows × 4 columns
asti_dsht_mean = asti_dsht.pivot_table(index='wind_deg')
asti_dsht_mean
humidity | temp | wind_speed | |
---|---|---|---|
wind_deg | |||
0.000 | 68.266667 | 24.882000 | 1.018667 |
10.000 | 62.666667 | 26.500000 | 3.600000 |
24.000 | 49.000000 | 30.790000 | 2.060000 |
30.000 | 73.000000 | 22.440000 | 2.050000 |
40.000 | 62.000000 | 27.680000 | 1.500000 |
42.501 | 66.000000 | 28.050000 | 2.570000 |
50.000 | 54.500000 | 27.760000 | 2.075000 |
70.000 | 56.750000 | 28.932500 | 2.600000 |
80.000 | 60.000000 | 22.680000 | 2.100000 |
90.000 | 61.500000 | 27.580000 | 1.800000 |
100.000 | 51.666667 | 31.138333 | 2.083333 |
110.000 | 46.000000 | 32.950000 | 2.100000 |
120.000 | 48.000000 | 32.080000 | 2.350000 |
140.000 | 45.000000 | 31.440000 | 1.500000 |
154.505 | 29.000000 | 28.560000 | 2.060000 |
170.000 | 58.000000 | 29.100000 | 5.700000 |
180.000 | 61.000000 | 26.870000 | 1.000000 |
240.000 | 46.000000 | 31.410000 | 2.100000 |
263.000 | 64.000000 | 29.510000 | 1.540000 |
270.000 | 61.000000 | 26.530000 | 2.600000 |
280.000 | 64.000000 | 23.160000 | 2.100000 |
290.000 | 64.000000 | 22.840000 | 1.500000 |
300.000 | 57.000000 | 26.290000 | 3.600000 |
310.000 | 73.000000 | 24.000000 | 1.500000 |
320.000 | 73.000000 | 21.590000 | 2.600000 |
321.501 | 84.000000 | 18.580000 | 0.880000 |
330.000 | 73.000000 | 24.216667 | 1.500000 |
340.000 | 74.333333 | 22.106667 | 2.600000 |
350.000 | 78.000000 | 23.036667 | 2.233333 |
360.000 | 74.333333 | 20.686667 | 1.900000 |
# 通过三个子画布绘制风向和风力、温度、湿度的关系
plt.figure(figsize=(15,6))
for i,column in enumerate(('wind_speed','humidity','temp')):
plt.subplot(1,3,i+1) # index 从1开始
plt.plot(asti_dsht_mean[column],c=np.random.random(size=3))
plt.title('风向 Vs '+ column,size=20)
plt.show()
角度 转成 弧度
- 角度/180 = 弧度/pi, 弧度 = 角度 * pi / 180
- asti_wind[(asti_wind[‘wind_deg’] >= deg) & (asti_wind[‘wind_deg’] < 45+deg)][‘wind_deg’]
由于风向是360度,我们可以考虑使用玫瑰图(极坐标条形图)
首先自定义一个画图函数
用numpy创建一个直方图,将360度划分为8个面元,将数据分类到这8个面元中
x = np.arange(360,step=45)
x
array([ 0, 45, 90, 135, 180, 225, 270, 315])
asti_ds_mean
wind_speed | |
---|---|
wind_deg | |
0.000 | 1.018667 |
10.000 | 3.600000 |
24.000 | 2.060000 |
30.000 | 2.050000 |
40.000 | 1.500000 |
42.501 | 2.570000 |
50.000 | 2.075000 |
70.000 | 2.600000 |
80.000 | 2.100000 |
90.000 | 1.800000 |
100.000 | 2.083333 |
110.000 | 2.100000 |
120.000 | 2.350000 |
140.000 | 1.500000 |
154.505 | 2.060000 |
170.000 | 5.700000 |
180.000 | 1.000000 |
240.000 | 2.100000 |
263.000 | 1.540000 |
270.000 | 2.600000 |
280.000 | 2.100000 |
290.000 | 1.500000 |
300.000 | 3.600000 |
310.000 | 1.500000 |
320.000 | 2.600000 |
321.501 | 0.880000 |
330.000 | 1.500000 |
340.000 | 2.600000 |
350.000 | 2.233333 |
360.000 | 1.900000 |
ds = asti_ds_mean.reset_index()
ds
wind_deg | wind_speed | |
---|---|---|
0 | 0.000 | 1.018667 |
1 | 10.000 | 3.600000 |
2 | 24.000 | 2.060000 |
3 | 30.000 | 2.050000 |
4 | 40.000 | 1.500000 |
5 | 42.501 | 2.570000 |
6 | 50.000 | 2.075000 |
7 | 70.000 | 2.600000 |
8 | 80.000 | 2.100000 |
9 | 90.000 | 1.800000 |
10 | 100.000 | 2.083333 |
11 | 110.000 | 2.100000 |
12 | 120.000 | 2.350000 |
13 | 140.000 | 1.500000 |
14 | 154.505 | 2.060000 |
15 | 170.000 | 5.700000 |
16 | 180.000 | 1.000000 |
17 | 240.000 | 2.100000 |
18 | 263.000 | 1.540000 |
19 | 270.000 | 2.600000 |
20 | 280.000 | 2.100000 |
21 | 290.000 | 1.500000 |
22 | 300.000 | 3.600000 |
23 | 310.000 | 1.500000 |
24 | 320.000 | 2.600000 |
25 | 321.501 | 0.880000 |
26 | 330.000 | 1.500000 |
27 | 340.000 | 2.600000 |
28 | 350.000 | 2.233333 |
29 | 360.000 | 1.900000 |
x = np.arange(360,step=45)
ds[(ds.wind_deg >=0) & (ds.wind_deg < 45)]
wind_deg | wind_speed | |
---|---|---|
0 | 0.000 | 1.018667 |
1 | 10.000 | 3.600000 |
2 | 24.000 | 2.060000 |
3 | 30.000 | 2.050000 |
4 | 40.000 | 1.500000 |
5 | 42.501 | 2.570000 |
x = np.arange(360,step=45)
speeds = [ds[(ds.wind_deg >=deg) & (ds.wind_deg < 45+deg)]['wind_speed'].mean()
for deg in x
]
speeds
[2.133111111111111,
2.2583333333333333,
2.0833333333333335,
3.0866666666666664,
1.0,
1.82,
2.2600000000000002,
1.9626666666666666]
x1 = x*np.pi /180
x1
array([0. , 0.78539816, 1.57079633, 2.35619449, 3.14159265,
3.92699082, 4.71238898, 5.49778714])
# 画极坐标轴
plt.subplot(1,1,1,polar=True)
plt.bar(x1,speeds,
color=np.random.random(size=(8,3)),
align='edge')
<BarContainer object of 8 artists>
# 设置宽度
plt.subplot(1,1,1,polar=True)
plt.bar(x1,speeds,
color=np.random.random(size=(8,3)),
width=0.7,
align='edge')
<BarContainer object of 8 artists>
plt.subplot(1,1,1,polar=True)
plt.bar(x1,speeds,
color=np.random.random(size=(8,3)),
width=0.7,
align='edge')
# 设置刻度
plt.xticks(x1,['东','东北','北','西北','西','西南','南','东南'])
([<matplotlib.projections.polar.ThetaTick at 0xaf8e930>,
<matplotlib.projections.polar.ThetaTick at 0xaedf590>,
<matplotlib.projections.polar.ThetaTick at 0xae54650>,
<matplotlib.projections.polar.ThetaTick at 0xae544b0>,
<matplotlib.projections.polar.ThetaTick at 0xade56d0>,
<matplotlib.projections.polar.ThetaTick at 0xade5af0>,
<matplotlib.projections.polar.ThetaTick at 0xade52f0>,
<matplotlib.projections.polar.ThetaTick at 0xade5030>],
<a list of 8 Text xticklabel objects>)
练习:计算米兰各个方向上的风速
full_df.city.unique()
array(['Asti', 'Bologna', 'Cesena', 'Faenza', 'Ferrara', 'Mantova',
'Milano', 'Piacenza', 'Ravenna', 'Torino'], dtype=object)
def draw_polar(full_df,city,compare_colname,title=None):
# 获取指定城市的 风向与某一列比较的信息
all_ = full_df.copy()
city_wind = all_[all_['city'] == city][['wind_deg',compare_colname]]
city_wind.sort_values('wind_deg',inplace=True) # 按方向排序
city_wind.reset_index(drop=True,inplace=True)
# 8个方向角度
degs = np.arange(0,360,step=45)
# 获取每个方向上的数据的平均值
heights = [city_wind[compare_colname][(city_wind['wind_deg'] >= deg) &
(city_wind['wind_deg'] < 45+deg)].mean() for deg in degs]
# 将角度转为弧度:角度/360 =弧度/2*pi
x = degs*np.pi/180
plt.figure(figsize=(6,6))
plt.axes(polar = True, frameon = True)
plt.bar(x,height=heights,
align='edge',
bottom=0.1,
width=0.75,
color=np.random.random(size=(8,3)))
plt.xticks(x,('东','东北','北','西北','西','西南','南','东南'),
fontsize=14)
if title:
plt.title(city+'城市的'+ title,fontsize=20,color='r',
loc='center',position=(0.5,1.1))
plt.show()
draw_polar(full_df,'Milano','wind_speed','风向与风速的关系')
draw_polar(full_df,'Milano','temp','风向与温度的关系')