1.Pandas簡介
Pandas是基于Numpy的一個開源Python庫,被廣泛用于快速分析數(shù)據(jù),以及數(shù)據(jù)清洗和準(zhǔn)備工作。
Pandas中有兩類重要的數(shù)據(jù)結(jié)構(gòu),就是序列Series和數(shù)據(jù)框DataFrame。
2.Series和DataFrame數(shù)據(jù)結(jié)構(gòu)
import numpy as np
import pandas as pd
s1 = pd.Series(np.array([1,2,3,4,5]))
print(s1)
print("***************")
s2 = pd.Series(np.array([1,2,3]), index=['e','b','c'])
print(s2)
3.查詢數(shù)據(jù)
查詢數(shù)據(jù)前五行
查詢數(shù)據(jù)的末尾5行
查詢指定的列
查詢指定的行
查詢指定的行和指定的列
多條件查詢
4,統(tǒng)計分析
Series總和,均值,最大最小值,中位數(shù),眾數(shù)
import numpy as np
import pandas as pd
a = np.random.normal(size=10)
d1 = pd.Series(2*a+3)
d2 = np.random.f(2,4,size=10)
d3 = np.random.randint(1,100,size=10)
print(d1)
print(d2)
print(d3)
print(d1.count()) #非空元素的計算
print(d1.min()) #最小值
print(d1.max()) #最大值
print(d1.idxmin()) #最小值的位置
print(d1.idxmax()) #最大值的位置
print(d1.sum()) #求和
print(d1.mean()) #均值
print(d1.median()) #中位值
print(d1.mode()) #眾數(shù)
print(d1.var()) #方差
print(d1.std()) #標(biāo)準(zhǔn)差
print(d1.describe()) #一次性輸出多個描述性統(tǒng)計指標(biāo)
#自定義一個函數(shù),將這些指標(biāo)全部匯總在一起
def stats(x):
return pd.Series([x.max(),x.min()],index=["最大值","最小值"])
print(stats(d1))
5.DataFrame:df.shape:維度
? ? ? ? ? ? ? ? ? ? df.info:數(shù)據(jù)表的基本信息
? ? ? ? ? ? ? ? ? ? df.dtypes:每一列數(shù)據(jù)的格式
? ? ? ? ? ? ? ? ? ? df.columns:查詢列的名稱
? ? ? ? df.corr()相關(guān)系數(shù)的求解
? ? ? ? df.corrwith()
? ? ? ? df.cov()協(xié)方差
pandas實現(xiàn)Sql操作
#pandas實現(xiàn)對數(shù)據(jù)的增刪改查
import pandas as pd
dict = {
"Name":["LiuShunxiang","Zhangshan"],
"Sex":["M","F"],
"Age":[27,23],
"Height":[165.7,167.2],
"Weight":[61,63]
}
print(dict)
student1 = pd.DataFrame(dict)
print(student1)
dict1 = {
"Name":["Liu","Zhang"],
"Sex":["M","F"],
"Age":[27,23],
"Height":[165.7,167.2],
"Weight":[61,63]
}
student2 = pd.DataFrame(dict1)
#將Student2中的數(shù)據(jù)增加到Student1中,通過concat數(shù)據(jù)實現(xiàn)
student3 = pd.concat([student1,student2],ignore_index="True") #對index無視
print(student3)
#增加新的列--增加的新列沒有賦值,就會出現(xiàn)NAN的形式
print(pd.DataFrame(student2,columns=['Age','Heihgt','Name','Sex','Weight','Score']))
#刪除Student2
#del student2
#print(student2)
print("*************************")
#刪除指定的行
#print(student3.drop([0]))
#查找25歲以下的學(xué)生
print(student3[student3["Age"]<25])
#刪除指定的列
print(student3.drop(['Height','Weight'],axis=1))
#不論刪除行還是列,都可以通過drop方法來實現(xiàn),只需要設(shè)定好刪除的軸即可,0刪除行,1刪除列。
print(student3)
#將Liu學(xué)生的身高改為173cm
student3.loc[student3['Name']=='Liu','Height']=173
print(student3)
#聚合groupby()
print(student3.groupby('Sex').mean())
print(student3.groupby(['Sex','Age']).mean())
#排序
print(student3.sort_values(by=['Sex','Age']))
#merge表的連接
merge
https://www.yiibai.com/pandas/python_pandas_merging_joining.html
6.缺失值處理
刪除法:當(dāng)數(shù)據(jù)中某個變量大部分值都會缺失時,可以考慮刪除變量;
? ? ? ? ? ? ? dropna完成。
替補法:對于連續(xù)變量,變量的分布近似或就是正態(tài)分布的話,可以用均值代替那些缺失值;
? ? ? ? ? ? ? ?如果變量是有偏的,可以使用中位數(shù)來代替那些缺失值
? ? ? ? ? ? ? 對于離散變量,一般使用眾數(shù)替換存在的缺失預(yù)測
? ? ? ? ? ? ?fillna完成。
?
7.實現(xiàn)數(shù)據(jù)透視表的功能
8.多層索引
?
?
?
?
?
?
?
?
?
?
?
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
