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

首頁 > 編程 > Python > 正文

python之pandas學習

2019-11-11 07:52:24
字體:
來源:轉載
供稿:網友

Python中的pandas模塊進行數據分析。

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

序列的創建主要有三種方式:

1)通過一維數組創建序列

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)通過字典的方式創建序列

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中的某一行或某一列創建序列

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

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)通過字典的方式創建數據框

以下以兩種字典來創建數據框,一個是字典列表,一個是嵌套字典。

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)通過數據框的方式創建數據框

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模塊為我們提供了非常多的描述性統計分析的指標函數,如總和、均值、最小值、最大值等,我們來具體看看這些函數: 首先隨機生成三組數據

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函數print(d1.idxmax()) #最大值的位置,類似于R中的which.max函數print(d1.quantile(0.1)) #10%分位數print(d1.sum()) #求和print(d1.mean()) #均值print(d1.median()) #中位數print(d1.mode()) #眾數print(d1.var()) #方差print(d1.std()) #標準差print(d1.mad()) #平均絕對偏差print(d1.skew()) #偏度print(d1.kurt()) #峰度print(d1.describe()) #一次性輸出多個描述性統計指標

實驗結果:

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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产女厕一区二区三区在线视 | 羞羞视频入口 | gril hd| 成人偷拍片视频在线观看 | 亚洲第一页在线观看 | 久久精品一区二区三区不卡牛牛 | 日韩av电影在线观看 | 成人免费看视频 | 一区在线视频观看 | 精品一区二区免费 | 深夜影院一级毛片 | 国产成人精品免高潮在线观看 | 精品国产一区二区三区四区在线 | 91看片在线观看视频 | 中文在线国产 | 精品成人免费视频 | 久久精品视频69 | 欧美日韩在线播放 | 久久久综合久久久 | 色吧久久 | 国产成人小视频在线观看 | hd性videos意大利复古 | 久久精品日产高清版的功能介绍 | 黄色网址在线视频 | 久久成人动漫 | 青草伊人网 | 中文字幕亚洲情99在线 | 曰韩一级片 | 国产自91精品一区二区 | 看免费一级毛片 | 欧美日韩在线视频一区 | 极品xxxx欧美一区二区 | www.成人免费 | 黄色大片免费看 | 精品黑人一区二区三区国语馆 | 国产免费v片 | 成人激情综合网 | 久久国产精品久久久久 | 久久久久久久久久网 | 亚欧美一区二区 | 国产精品久久在线观看 |