99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁大數(shù)據(jù)時代pandas模塊,我覺得掌握這些就夠用了!
pandas模塊,我覺得掌握這些就夠用了!
2020-07-20
收藏

pandas  是為了解決數(shù)據(jù)分析任務(wù)而創(chuàng)建的Python 庫,源于NumPy ,經(jīng)常被用于對數(shù)據(jù)挖掘前期數(shù)據(jù)的處理工作。pandas提供了大量的處理數(shù)據(jù)的函數(shù)和方法,并且還納入了大量庫與很多標準的數(shù)據(jù)模型,能讓我們更加高效地處理大型數(shù)據(jù)集。但是大家在使用pandas的過程中,經(jīng)常會遇到這樣那樣的問題,下面,小編就整理了一些大家需要掌握的pandas 的基礎(chǔ)知識,希望對大家有所幫助。

以下文章來源于: 數(shù)據(jù)分析1480

作者:劉順祥

背景介紹        

經(jīng)常會有一些朋友問我類似的問題,“哎呀,這個數(shù)據(jù)該怎么處理啊,我希望結(jié)果是這樣的,麻煩劉老師幫我看看?!薄ⅰ皠⒗蠋?,怎么把一列數(shù)據(jù)拆分出來,并取出最后一個拆分結(jié)果呀?”、“劉老師,怎么將Json數(shù)據(jù)讀入到Python中呢?”。在我看來,這些問題都可以借助于Pandas模塊完成,因為Pandas屬于專門做數(shù)據(jù)預(yù)處理的數(shù)據(jù)科學(xué)包。下面來介紹一下我認為Pandas模塊中需要掌握的功能和函數(shù)。

        數(shù)據(jù)讀寫        

          案例演示          

# 讀入MySQL數(shù)據(jù)庫數(shù)據(jù)
# 導(dǎo)入第三方模塊
import pymysql

# 連接MySQL數(shù)據(jù)庫
conn = pymysql.connect(host='localhost', user='root', password='test', 
                       database='test', port=3306, charset='utf8')
# 讀取數(shù)據(jù)
user = pd.read_sql('select * from topy', conn)
# 關(guān)閉連接
conn.close()
# 數(shù)據(jù)輸出
User

        數(shù)據(jù)初印象        

          案例演示          

# 數(shù)據(jù)讀取
sec_cars = pd.read_table(r'C:\Users\Administrator\Desktop\sec_cars.csv', sep = ',')
# 預(yù)覽數(shù)據(jù)的前五行
sec_cars.head()

# 查看數(shù)據(jù)的行列數(shù)
print('數(shù)據(jù)集的行列數(shù):\n',sec_cars.shape)

# 查看數(shù)據(jù)集每個變量的數(shù)據(jù)類型
print('各變量的數(shù)據(jù)類型:\n',sec_cars.dtypes)

# 數(shù)據(jù)的描述性統(tǒng)計
sec_cars.describe()

        數(shù)據(jù)清洗        

          案例演示          

# 數(shù)據(jù)讀入
df = pd.read_excel(r'C:\Users\Administrator\Desktop\data_test05.xlsx')
# 缺失觀測的檢測
print('數(shù)據(jù)集中是否存在缺失值:\n',any(df.isnull()))

# 刪除法之記錄刪除
df.dropna()
# 刪除法之變量刪除
df.drop('age', axis = 1)

# 替換法之前向替換
df.fillna(method = 'ffill')
# 替換法之后向替換
df.fillna(method = 'bfill')

# 替換法之常數(shù)替換
df.fillna(value = 0)
# 替換法之統(tǒng)計值替換
df.fillna(value = {'gender':df.gender.mode()[0], 'age':df.age.mean(),     
                   'income':df.income.median()})

    類型轉(zhuǎn)換與元素及運算    

          案例演示          

# 數(shù)據(jù)讀入
df = pd.read_excel(r'C:\Users\Administrator\Desktop\data_test03.xlsx')

# 將birthday變量轉(zhuǎn)換為日期型
df.birthday = pd.to_datetime(df.birthday, format = '%Y/%m/%d')

# 將手機號轉(zhuǎn)換為字符串
df.tel = df.tel.astype('str')

# 新增年齡和工齡兩列
df['age'] = pd.datetime.today().year - df.birthday.dt.year
df['workage'] = pd.datetime.today().year - df.start_work.dt.year

# 將手機號中間四位隱藏起來
df.tel = df.tel.apply(func = lambda x : x.replace(x[3:7], '****'))
# 取出郵箱的域名
df['email_domain'] = df.email.apply(func = lambda x : x.split('@')[1])

# 取出人員的專業(yè)信息
df['profession'] = df.other.str.findall('專業(yè):(.*?),')
# 去除birthday、start_work和other變量
df.drop(['birthday','start_work','other'], axis = 1, inplace = True)

數(shù)據(jù)合并、連接與匯總

          案例演示          

# 構(gòu)造數(shù)據(jù)集df1和df2
df1 = pd.DataFrame({'name':['張三','李四','王二'], 'age':[21,25,22], 
                  'gender':['男','女','男']})
df2 = pd.DataFrame({'name':['丁一','趙五'], 'age':[23,22], 'gender':['女','女']})
# 數(shù)據(jù)集的縱向合并
pd.concat([df1,df2] , keys = ['df1','df2'])

# 如果df2數(shù)據(jù)集中的“姓名變量為Name”
df2 = pd.DataFrame({'Name':['丁一','趙五'], 'age':[23,22], 'gender':['女','女']})
# 數(shù)據(jù)集的縱向合并
pd.concat([df1,df2])

# 構(gòu)造數(shù)據(jù)集
df3 = pd.DataFrame({'id':[1,2,3,4,5],'name':['張三','李四','王二','丁一','趙五'],
                  'age':[27,24,25,23,25],'gender':['男','男','男','女','女']})
df4 = pd.DataFrame({'Id':[1,2,2,4,4,4,5], 'score':[83,81,87,75,86,74,88]
                  'kemu':['科目1','科目1','科目2','科目1','科目2','科目3','科目1']})
df5 = pd.DataFrame({'id':[1,3,5],'name':['張三','王二','趙五'],
                  'income':[13500,18000,15000]})
# 三表的數(shù)據(jù)連接
# 首先df3和df4連接
merge1 = pd.merge(left = df3, right = df4, how = 'left', left_on='id', right_on='Id')
merge1
# 再將連接結(jié)果與df5連接
merge2 = pd.merge(left = merge1, right = df5, how = 'left')
merge2

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }