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

首頁 > 編程 > Python > 正文

python之pandas學習

2019-11-11 07:46:01
字體:
供稿:網(wǎng)友

Python中的pandas模塊進行數(shù)據(jù)分析。

接下來pandas介紹中將學習到如下8塊內(nèi)容: 1、數(shù)據(jù)結構簡介:DataFrame和Series 2、數(shù)據(jù)索引index 3、利用pandas查詢數(shù)據(jù) 4、利用pandas的DataFrames進行統(tǒng)計分析 5、利用pandas實現(xiàn)SQL操作 6、利用pandas進行缺失值的處理 7、利用pandas實現(xiàn)Excel的數(shù)據(jù)透視表功能 8、多層索引的使用 一、數(shù)據(jù)結構介紹 在pandas中有兩類非常重要的數(shù)據(jù)結構,即序列Series和數(shù)據(jù)框DataFrame。Series類似于numpy中的一維數(shù)組,除了通吃一維數(shù)組可用的函數(shù)或方法,而且其可通過索引標簽的方式獲取數(shù)據(jù),還具有索引的自動對齊功能;DataFrame類似于numpy中的二維數(shù)組,同樣可以通用numpy數(shù)組的函數(shù)和方法,而且還具有其他靈活應用,后續(xù)會介紹到。 1、Series的創(chuàng)建

序列的創(chuàng)建主要有三種方式:

1)通過一維數(shù)組創(chuàng)建序列

import numpy as np, pandas as pdarr1 = np.arange(10)PRint(arr1)print(type(arr1))s1 = pd.Series(arr1)print(s1)print(type(s1))

實驗結果:

[0 1 2 3 4 5 6 7 8 9]<class 'numpy.ndarray'>0 01 12 23 34 45 56 67 78 89 9dtype: int32<class 'pandas.core.series.Series'>

2)通過字典的方式創(chuàng)建序列

dic1 = {'a':10,'b':20,'c':30,'d':40,'e':50}print(dic1)print(type(dic1))s2 = pd.Series(dic1)print(s2)print(type(s2))

實驗結果:

{'c': 30, 'b': 20, 'e': 50, 'a': 10, 'd': 40}<class 'dict'>a 10b 20c 30d 40e 50dtype: int64<class 'pandas.core.series.Series'>

3)通過DataFrame中的某一行或某一列創(chuàng)建序列

這部分內(nèi)容我們放在后面講,因為下面就開始將DataFrame的創(chuàng)建。 2、DataFrame的創(chuàng)建 數(shù)據(jù)框的創(chuàng)建主要有三種方式: 1)通過二維數(shù)組創(chuàng)建數(shù)據(jù)框

import numpy as np, pandas as pdarr2 = np.array(np.arange(12)).reshape(4,3)print(arr2)print(type(arr2))df1 = pd.DataFrame(arr2)print(df1)print(type(df1))

實驗結果:

[[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]]<class 'numpy.ndarray'> 0 1 20 0 1 21 3 4 52 6 7 83 9 10 11<class 'pandas.core.frame.DataFrame'>

2)通過字典的方式創(chuàng)建數(shù)據(jù)框

以下以兩種字典來創(chuàng)建數(shù)據(jù)框,一個是字典列表,一個是嵌套字典。

dic2 = {'a':[1,2,3,4],'b':[5,6,7,8],'c':[9,10,11,12],'d':[13,14,15,16]}print(dic2)print(type(dic2))df2 = pd.DataFrame(dic2)print(df2)print(type(df2))dic3 = {'one':{'a':1,'b':2,'c':3,'d':4},'two':{'a':5,'b':6,'c':7,'d':8},'three':{'a':9,'b':10,'c':11,'d':12}}print(dic3)print(type(dic3))df3 = pd.DataFrame(dic3)print(df3)print(type(df3))

實驗結果:

{'d': [13, 14, 15, 16], 'b': [5, 6, 7, 8], 'a': [1, 2, 3, 4], 'c': [9, 10, 11, 12]}<class 'dict'> a b c d0 1 5 9 131 2 6 10 142 3 7 11 153 4 8 12 16<class 'pandas.core.frame.DataFrame'>{'two': {'d': 8, 'b': 6, 'a': 5, 'c': 7}, 'one': {'d': 4, 'b': 2, 'a': 1, 'c': 3}, 'three': {'d': 12, 'b': 10, 'a': 9, 'c': 11}}<class 'dict'> one three twoa 1 9 5b 2 10 6c 3 11 7d 4 12 8<class 'pandas.core.frame.DataFrame'>

3)通過數(shù)據(jù)框的方式創(chuàng)建數(shù)據(jù)框

dic3 = {'one':{'a':1,'b':2,'c':3,'d':4},'two':{'a':5,'b':6,'c':7,'d':8},'three':{'a':9,'b':10,'c':11,'d':12}}print(dic3)print(type(dic3))df3 = pd.DataFrame(dic3)# print(df3)# print(type(df3))df4 = df3[['one','three']]print(df4)print(type(df4))s3 = df3['one']print(s3)print(type(s3))

實驗結果:

{'one': {'d': 4, 'b': 2, 'a': 1, 'c': 3}, 'three': {'d': 12, 'b': 10, 'a': 9, 'c': 11}, 'two': {'d': 8, 'b': 6, 'a': 5, 'c': 7}}<class 'dict'> one threea 1 9b 2 10c 3 11d 4 12<class 'pandas.core.frame.DataFrame'>a 1b 2c 3d 4Name: one, dtype: int64<class 'pandas.core.series.Series'>

pandas模塊為我們提供了非常多的描述性統(tǒng)計分析的指標函數(shù),如總和、均值、最小值、最大值等,我們來具體看看這些函數(shù): 首先隨機生成三組數(shù)據(jù)

np.random.seed(1234)d1 = pd.Series(2*np.random.normal(size = 10)+3)print(d1)d2 = np.random.f(2,4,size = 10)print(d2)d3 = np.random.randint(1,100,size = 10)print(d3)print(d1.count()) #非空元素計算print(d1.min()) #最小值print(d1.max()) #最大值print(d1.idxmin()) #最小值的位置,類似于R中的which.min函數(shù)print(d1.idxmax()) #最大值的位置,類似于R中的which.max函數(shù)print(d1.quantile(0.1)) #10%分位數(shù)print(d1.sum()) #求和print(d1.mean()) #均值print(d1.median()) #中位數(shù)print(d1.mode()) #眾數(shù)print(d1.var()) #方差print(d1.std()) #標準差print(d1.mad()) #平均絕對偏差print(d1.skew()) #偏度print(d1.kurt()) #峰度print(d1.describe()) #一次性輸出多個描述性統(tǒng)計指標

實驗結果:

0 3.9428701 0.6180492 5.8654143 2.3746964 1.5588235 4.7743266 4.7191777 1.7269538 3.0313939 -1.485370dtype: float64[ 2.95903083 0.32784914 2.27321231 0.05147861 9.10291941 0.15691116 0.99021894 1.84169938 0.32196418 0.04276792][57 71 57 87 45 91 84 48 50 19]10-1.485369908375.86541393685920.407706758691282927.12633015112.712633015112.703044476022716Series([], dtype: float64)4.917719121012.217593091851.75400292821-0.453758801844-0.116260760058count 10.000000mean 2.712633std 2.217593min -1.48537025% 1.60085550% 2.70304475% 4.525100max 5.865414dtype: float64
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产91免费看 | 久久tv免费国产高清 | 日韩视频在线观看免费视频 | 色啪综合 | 久久久久99精品 | 成年片在线观看 | 欧美一级成人 | xp123精品视频 | 久久性生活免费视频 | 全免费午夜一级毛片真人 | 免费欧美精品 | 一级做受大片免费视频 | 国产女做a爱免费视频 | 精品国产乱码一区二区三区四区 | 免费a视频在线观看 | 国产午夜免费福利 | 一级黄色国产视频 | 久久久久免费精品 | 久草在线观看资源 | 黄色免费电影网址 | 在线免费黄色网 | 涩涩屋av| 国产精品区一区二区三区 | 超碰在线97国产 | 久久精品影视 | 91精品国产网站 | 免费观看亚洲视频 | 中文日韩字幕 | cosplay裸体福利写真 | 国产流白浆高潮在线观看 | 毛片哪里看| 亚洲日本韩国精品 | 亚洲福利在线视频 | 国产资源在线观看 | 国产一级一国产一级毛片 | 136福利视频| 羞羞的视频在线 | 色蜜桃av | omofun 动漫在线观看 | 狠狠撸电影 | 免费a级毛片永久免费 |