Python數(shù)據(jù)分析與處理(二)——處理中國地區(qū)信息
2.1數(shù)據(jù)的爬取
代碼:
import pandas as pd data=pd.read_csv("example_data.csv",header=1) print(data) data1=pd.read_csv("北京地區(qū)信息.csv",header=1,encoding='gbk') data2=pd.read_csv("天津地區(qū)信息.csv",encoding='gbk') print(data1) print(data2)
代碼運行結果:
首先使用pandas
的read_csv()
方法進行數(shù)據(jù)的讀取,然后就能夠看到相應的表格信息。
2.2檢查重復數(shù)據(jù)
dupnum=data.duplicated() print(dupnum) \# 對重復值進行處理 caldup=data.drop_duplicates() print(caldup)
代碼運行結果:
主要是是使用這個duplicated()
方法進行數(shù)據(jù)的查重,返回一個布爾序列,僅對唯一元素而言為True
。如果有重復的數(shù)據(jù)就會在該數(shù)值的部分返貨Flase
。
然后我們就可以使用drop_duplicates()
進行重復值刪除。
2.3檢查缺失值
代碼:
from pandas import Series from numpy import NAN \# import pandas as pd series_obj=Series([1,None]) pd.notnull(series_obj) \# 上面做的是測試 pd.notnull(data) pd.notnull(data1) pd.notnull(data2)
代碼運行結果:
使用pd.notnull(data1)
進行非空數(shù)值的返回, 返回值是布爾型的矩陣,再取df[布爾型矩陣]返回的是id為非空的行。
2.4 檢查異常值
import numpy as np \# 2.4 檢查異常值 def three_sig(ser1): mean_value=ser1.mean() \# 標準差 std_value=ser1.std() \# 位于3σ范圍外的都是異常值 \# 數(shù)值大于u+3σ小雨u-3σ rule=(mean_value-3*std_value>ser1)|(ser1.mean()+3*ser1.std()<ser1) index=np.arange(ser1.shape[0])[rule] outrange=ser1.iloc[index] return outrange three_sig(data2["女性"])
代碼運行結果:
3σ原則又稱為拉依達準則,該準則具體來說,就是先假設一組檢測數(shù)據(jù)只含有隨機誤差,對原始數(shù)據(jù)進行計算處理得到標準差,然后按一定的概率確定一個區(qū)間,認為誤差超過這個區(qū)間的就屬于異常值。
通俗理解就是正態(tài)分布。
到此這篇關于Python數(shù)據(jù)分析與處理--處理中國地區(qū)信息的文章就介紹到這了,更多相關Python Python數(shù)據(jù)分析與處理內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Pytorch自己加載單通道圖片用作數(shù)據(jù)集訓練的實例
今天小編就為大家分享一篇Pytorch自己加載單通道圖片用作數(shù)據(jù)集訓練的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01python tkinter中的錨點(anchor)問題及處理
這篇文章主要介紹了python tkinter中的錨點(anchor)問題及處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06