麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Python > 正文

用Python處理"大"XLS文件

2019-11-11 07:53:36
字體:
來源:轉載
供稿:網友

權當學習Python練手用的.

數據來data.gov.uk,大小有58.4MB

文件都是些什么內容?

’Accident_Index’, ‘Location_Easting_OSGR’,‘Location_Northing_OSGR’, ‘Longitude’, ‘Latitude’, ‘Police_Force’, ‘Accident_Severity’, ‘Number_of_Vehicles’, ‘Number_of_Casualties’, ‘Date’, ‘Day_of_Week’, ‘Time’, ‘Local_Authority_(District)’, ‘Local_Authority_(Highway)’, ‘1st_Road_Class’, ‘1st_Road_Number’, ‘Road_Type’, ‘Speed_limit’, ‘Junction_Detail’, ‘Junction_Control’, ‘2nd_Road_Class’, ‘2nd_Road_Number’, ‘Pedestrian_Crossing-Human_Control’, ‘Pedestrian_Crossing_Physical_Facilities’, ’Light_Conditions’, ‘Weather_Conditions’, ‘Road_Surface_Conditions’, ‘Special_Conditions_at_Site’, ‘Carriageway_Hazards’, ‘Urban_or_Rural_Area’, ‘Did_Police_Officer_Attend_Scene_of_Accident’, ‘LSOA_of_Accident_Location’

這里寫圖片描述

LowMemory 方式讀取文件

#read the filefiledir='/home/derek/Desktop/python-data-analyis/large-Excel-files/Accidents_2013.csv'data = pd.read_csv(filedir,low_memory=False)PRint data.ix[:10]['Day_of_Week']SQL likes 提取數據信息print 'Accidents'print '----------'#選擇星期日發生的事故accidents_sunday = data[data.Day_of_Week==1]print 'Accidents which happended on a Sunday: ',len(accidents_sunday)#選擇星期日發生的且涉事人數在十人以上的事故accidents_sunday_twenty_cars = data[(data.Day_of_Week==1) & (data.Number_of_Vehicles>10)]print'Accidents which happened on a Sunday involving > 10 cars: ' , len(accidents_sunday_twenty_cars)#選擇星期日發生的且涉事人數在十人以上且天氣情況是下雨的事故(2對應的是無風下雨)accidents_sunday_twenty_cars_rain = data[(data.Day_of_Week==1) & (data.Number_of_Vehicles>10) & (data.Weather_Conditions==2)]print'Accidents which happened on a Sunday involving > 10 cars with rainning: ' , len(accidents_sunday_twenty_cars_rain)#選擇在倫敦的星期日發生的事故london_data = data[(data['Police_Force'] == 1) & (data.Day_of_Week==1)]print 'Accidents in London on a Sunday',len(london_data)#選擇在2000年的倫敦的星期日發生的事故london_data_2000 = london_data[((pd.to_datetime('2000-1-1', errors='coerce')) > (pd.to_datetime(london_data['Date'],errors='coerce'))) & (pd.to_datetime(london_data['Date'],errors='coerce') < (pd.to_datetime('2000-12-31', errors='coerce')))]print 'Accidents in London on a Sunday in 2000:',len(london_data_2000)

給人的感覺是特別像SQL語句,DataFrame的這種切片,方式特別好用,對不對?

pd.to_datetime(london_data['Date'],errors='coerce')

這里是日期轉換函數.

輸出:

Accidents----------Accidents which happended on a Sunday: 14854Accidents which happened on a Sunday involving > 10 cars: 1Accidents which happened on a Sunday involving > 10 cars with rainning: 1Accidents in London on a Sunday 2374Accidents in London on a Sunday in 2000: 0

將部分DataFrame數據以XLSX文件存儲下來 確保你安裝了XlsxWriter

sudo pip install XlsxWriter

writer = pd.ExcelWriter('london_data.xlsx', engine='xlsxwriter')london_data.to_excel(writer, 'sheet1')writer.save()writer.close()塊讀取,分析一個星期中那一天最有出事故的概率最大 代碼.2013,2014,2015三年的事故記錄,在’Accidents_2013.csv’,’Accidents_2014.csv’, ‘Accidents_2015.csv’這三個文件中import pandas as pdfrom pandas import Seriesimport matplotlib.pyplot as plt#read the filedir='/home/derek/Desktop/python-data-analyis/large-excel-files/'filedir=['Accidents_2013.csv','Accidents_2014.csv', 'Accidents_2015.csv']tot = Series([])for i in range(3): #塊讀取文件, 每次讀1000條記錄 data = pd.read_csv(dir + filedir[i],chunksize=1000) for piece in data: tot = tot.add(piece['Day_of_Week'].value_counts(), fill_value=0)day_index = ['Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat']print 'data like:'#tot = tot.sort_values(ascending=False)print tot#重新構造一個Series,是為了給索引命名new_Series = Series(tot.values, index=day_index)new_Series.plot()plt.show()plt.close()

控制臺輸出:

data like:1 460522 609563 650064 640395 644456 693787 55162dtype: float64

圖: 這里寫圖片描述 三年記錄在案的有425038條記錄.

結論: 看來,英國人在工作日出行要比在休息日造成更多的事故.星期五的出行造成的事故最多,或許,星期五急著回家,哈哈.相比起來,星期五不適合外出.

參考文章來源

文件沒有提供,是因為:讀者可以自己去下載,可能找到更想更好用Python分析的數據.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产精品久久久久久影视 | 日本高清黄色片 | 一级黄色影院 | 欧美一级黄 | 91久久国产露脸精品国产护士 | 国产一区二区三区在线视频 | 色婷婷av一区二区三区久久 | 羞羞视频免费观看网站 | 2021狠狠操 | 欧美激情视频一区二区免费 | 精精国产xxxx视频在线野外 | 日韩美香港a一级毛片 | 美国一级毛片片aa久久综合 | 国产羞羞视频在线观看免费应用 | 国产精品刺激对白麻豆99 | 一级毛片手机在线观看 | 一级黄色免费观看 | 91av在线免费 | 日本黄色一级毛片 | 韩国美女一区 | 精品一区二区三区免费视频 | 久久国产精 | 国产一级一片免费播放 | 久久密| 四季久久免费一区二区三区四区 | 特级黄色一级毛片 | 久久精品影视 | 久色精品| 久久亚洲精品久久国产一区二区 | a视频在线播放 | 成人在线视频网 | 成人免费观看在线 | 亚洲自拍第一 | 国产1区在线 | 久久一本日日摸夜夜添 | 1314成人网| 精品国产91久久久久久久妲己 | 高清在线国产 | 国产精品久久77777 | 久久久久久久一区 | 99激情|