前90天資料存字典

2021-10-05 03:55:45 字數 4065 閱讀 9710

#本**使用2017-06-01到2017-08-29共30+31+29=90天,unix時間戳是1496246400到1504022400(2017-08-30)零點

#一天時間是86400

import plotly

import chart_studio.plotly as pyf

import plotly.graph_objs as go

import numpy as np

import pandas as pd

import math

from matplotlib.path import path

import numpy as np

import plotly.offline as of

import plotly.graph_objs as go

import plotly.plotly as py

import numpy as np

import pandas as pd

import folium

import webbrowser

from folium.plugins import heatmap

import datetime

import time

import pymysql.cursors

import decimal

import geohash

import geohash

defgeo_demand

(geo_top_geo,cx_time)

: conn = pymysql.connect(

host=

'localhost'

, port=

3306

, user=

'root'

, passwd=

'xu19931026'

, db=

'hk_taxi'

, charset=

'utf8'

) cursor = conn.cursor(

)# 獲取游標

sql =

"select geohash,number,cast(number/(select count( 0 ) from haikou_1 where unix_timestamp(departure_time)<%s and unix_timestamp(departure_time)>1496246400 and left(`geo`,5)=%s) as char(15))as demand from (select left(`geo`,6) as geohash,count(0) as number from haikou_1 where unix_timestamp(departure_time)<%s and unix_timestamp(departure_time)>1496246400 and left(`geo`,5)=%s group by geohash order by number desc) as t"

cursor.execute(sql,

(cx_time,geo_top_geo,cx_time,geo_top_geo)

) conn.commit(

) result = cursor.fetchall(

) df =

list

(result)

# 將元組轉換為列表

conn.close(

)# 關閉資料庫連線

geo_dict =

# 字典

geo_list =

order_number =

demand =

for point in df:

(point[0]

))# 將字串的經緯度轉換為float格式1]

)# 50610之類的本身就是int

float

(point[2]

))# decimal轉為float

num =

len(geo_list)

# num為陣列大小

geo_data =

#放到字典中

geo_nb =

[[geo_list[i]

, order_number[i]

]for i in

range

(num)

]return geo_data #返回geohash對應的訂單量與訂單量百分比即demand

defget_geohash

(lon, lat)

:#生成geohash

geo = geohash.encode(lat, lon)

# precision=9可以加精度

return geo

defjie_geohash

(a):

#解碼a

#生成geohash

b=b=geohash.decode(a)

# precision=9可以加精度

return b

if __name__ ==

'__main__'

: taxi_geo1 =

'w7w3y2cjsg98'

# 司機位置座標所在的的geo

taxi_geo = taxi_geo1[0:

5]# 取司機的前5位geohash

central_points =

list

(jie_geohash(taxi_geo)

)# 生成中心點並轉為列表(30.623703002929688, 104.03160095214844)

lats_jia6=

0.0027

# geohash為6時候誤差

lons_jia6 =

0.0055

lats_jia5 =

0.022

lons_jia5 =

0.022

cx_time=

1496246400

#2017-06-01到2017-08-29共30+31+29=90天unix時間戳是1496246400到1504022400(2017-08-30)零點

#一天時間是86400

dic_all =

z=1while z<91:

#查詢前4天

cx_time +=

86400

geo_data = geo_demand(taxi_geo,cx_time)

dic_all[z]

= geo_data #全部存放在字典中

z+=1if z%

10==0:

print

('已處理'

+str

(z)+

'條資料'

)print

(dic_all)

print

('*****最終結果,請儲存*****'

)print

(dic_all)

#最終結果

#, 2: , 3: , 4: , 5: , 6: , 7: , 8: , 9: , 10: , 11: , 12: , 13: , 14: , 15: , 16: , 17: , 18: , 19: , 20: , 21: , 22: , 23: , 24: , 25: , 26: , 27: , 28: , 29: , 30: , 31: , 32: , 33: , 34: , 35: , 36: , 37: , 38: , 39: , 40: , 41: , 42: , 43: , 44: , 45: , 46: , 47: , 48: , 49: , 50: , 51: , 52: , 53: , 54: , 55: , 56: , 57: , 58: , 59: , 60: , 61: , 62: , 63: , 64: , 65: , 66: , 67: , 68: , 69: , 70: , 71: , 72: , 73: , 74: , 75: , 76: , 77: , 78: , 79: , 80: , 81: , 82: , 83: , 84: , 85: , 86: , 87: , 88: , 89: , 90: }

資料庫取前N天資料

oracle資料庫中 首先要區分,時間欄位是日期格式,還是字串 日期格式 trunc sysdate 1 字串格式 to char sysdate 30,yyyy mm dd 選前1天資料 select from 表 where 日期 to date to char sysdate 1,yyyy m...

第6天 資料Array

every 方法測試陣列的所有元素是否都通過了指定函式的測試。array.every callback thisarg callback 被呼叫時傳入三個引數 元素值,元素的索引,原陣列 filter 方法建立乙個新陣列,其包含通過所提供函式實現的測試的所有元素。push 方法將乙個或多個元素新增到...

210天資料探勘學習計畫

在學習的過程中,我會實時更新計畫的實現情況。在學習的過程中,我會實時更新計畫的實現情況。最近更新 第9天 2020.07.06 第01周 leetcode簡單題刷題計畫 leetcode簡單題150個 6 25 完成 leetcode簡單題192個 中等題1個 第001天 2020.06.28 le...