小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Python Pandas 中的數(shù)據(jù)結(jié)構(gòu)(超詳細,你確定不學習一下?)

 禁忌石 2022-05-07 發(fā)布于浙江省

Pandas有三種數(shù)據(jù)結(jié)構(gòu):Series、DataFrame和Panel。Series類似于數(shù)組;DataFrame類似于表格;Panel可視為Excel的多表單Sheet

文章圖片1

1.Series

Series是一種一維數(shù)組對象,包含一個值序列,并且包含數(shù)據(jù)標簽,稱為索引(index),通過索引來訪問數(shù)組中的數(shù)據(jù)。

1.1通過列表創(chuàng)建Series

例1.通過列表創(chuàng)建

import pandas as pdobj = pd.Series([1,-2,3,4]) #僅由一個數(shù)組構(gòu)成print(obj)
out:0 11 -22 33 4dtype: int64

輸出的第一列為index,第二列為數(shù)據(jù)value。如果創(chuàng)建Series時沒有指定index,Pandas會采用整型數(shù)據(jù)作為該Series的index。也可以使用Python里的索引index和切片slice技術(shù)

例2.創(chuàng)建Series時指定索引

import pandas as pdi = ['a','c','d','a']v = [2,4,5,7]t = pd.Series(v,index=i,name='col')print(t)
out:a 2c 4d 5a 7Name: col, dtype: int64

盡管創(chuàng)建Series指定了index,實際上Pandas還是有隱藏的index位置信息。所以Series有兩套描述某條數(shù)據(jù)手段:位置和標簽

例3.Series位置和標簽的使用

import pandas as pdval = [2,4,5,6]idx1 = range(10,14)idx2 = 'hello the cruel world'.split()s0 = pd.Series(val)s1 = pd.Series(val,index=idx1)t = pd.Series(val,index=idx2)print(s0.index)print(s1.index)print(t.index)print(s0[0])print(s1[10])print('default:',t[0],'label:',t['hello'])

1.2通過字典創(chuàng)建Series

如果數(shù)據(jù)被存放在一個Python字典中,也可以直接通過這個字典來創(chuàng)建Series

例4.通過字典創(chuàng)建Series

import pandas as pdsdata = {'Ohio':35000,'Texass':71000,'Oregon':16000,'Utah':5000}obj = pd.Series(sdata)print(obj)
Ohio 35000Texass 71000Oregon 16000Utah 5000dtype: int64

如果只傳入一個字典,則結(jié)果Series中的索引就是原字典的鍵(有序排列)

例5.通過字典創(chuàng)建Series時的索引

import pandas as pdsdata = {'a':100,'b':200,'e':300}obj = pd.Series(sdata)print(obj)
a 100b 200e 300dtype: int64

如果字典中的鍵值和指定的索引不匹配,則對應(yīng)的值時NaN

例6.鍵值和指定索引不匹配

import pandas as pdsdata = {'a':100,'b':200,'e':300}letter = ['a','b','c','e']obj = pd.Series(sdata,index=letter)print(obj)
a 100.0b 200.0c NaNe 300.0dtype: float64

對于許多應(yīng)用而言,Series重要的一個功能是:它在算術(shù)運算中會自動對齊不同索引的數(shù)據(jù)

例7.不同索引數(shù)據(jù)的自動對齊

import pandas as pdsdata = {'Ohio':35000,'Texas':71000,'Oregon':16000,'Utah':5000}obj1 = pd.Series(sdata)states = ['California','Ohio','Oregon','Texas']obj2 = pd.Series(sdata,index=states)print(obj1+obj2)
California NaNOhio 70000.0Oregon 32000.0Texas 142000.0Utah NaNdtype: float64

Series的索引可以通過賦值的方式就地修改

例8.Series索引的修改

import pandas as pdobj = pd.Series([4,7,-3,2])obj.index = ['Bob','Steve','Jeff','Ryan']print(obj)
Bob 4Steve 7Jeff -3Ryan 2dtype: int64

2.DataFrame

DataFrame是一個表格型的數(shù)據(jù)結(jié)構(gòu),它含有一組有序的列,每列可以是不同類型的值(數(shù)值、字符串、布爾值等)。DataFrame既有行索引也有列索引,它可以被看作由Series組成的字典(共用同一個索引)。跟其他類型的數(shù)據(jù)結(jié)構(gòu)相比,DataFrame中面向行和面向列的操作上基本上是平衡的

構(gòu)建DataFrame的方式有很多,最常用的是直接傳入一個由等長列表或NumPy數(shù)組組成的字典來形成DataFrame

例9.DataFrame的創(chuàng)建

import pandas as pddata = {'name':['張三','李四','王五','小明'],'sex':['female','female','male','male'],'year':[2001,2001,2003,2002],'city':['北京','上海','廣州','北京']}df = pd.DataFrame(data)print(df)
name sex year city0 張三 female 2001 北京1 李四 female 2001 上海2 王五 male 2003 廣州3 小明 male 2002 北京

DataFrame會自動加上索引(跟Series一樣),且全部列會被有序排列。如果指定了列名序列,則DataFrame的列就會按照指定順序進行排列

例10.DataFrame的索引

import pandas as pddata = {'name':['張三','李四','王五','小明'],'sex':['female','female','male','male'],'year':[2001,2001,2003,2002],'city':['北京','上海','廣州','北京']}df = pd.DataFrame(data,columns = ['name','year','sex','city'])print(df)
name year sex city0 張三 2001 female 北京1 李四 2001 female 上海2 王五 2003 male 廣州3 小明 2002 male 北京

跟Series一樣,如果傳入的列在數(shù)據(jù)中找不到,就會產(chǎn)生NaN值。


例11.DataFrame創(chuàng)建時的空缺值

import pandas as pddata = {    'name':['張三','李四','王五','小明'],    'sex':['female','female','male','male'],    'year':[2001,2001,2003,2002],    'city':['北京','上海','廣州','北京']}df = pd.DataFrame(data,columns = ['name','year','sex','city','address'])print(df)
name year sex city address0 張三 2001 female 北京 NaN1 李四 2001 female 上海 NaN2 王五 2003 male 廣州 NaN3 小明 2002 male 北京 NaN

DataFrame構(gòu)造函數(shù)的columns函數(shù)給出列的名字,index給出label標簽

例12.DataFrame構(gòu)建時指定列名

import pandas as pddata = {'name':['張三','李四','王五','小明'],'sex':['female','female','male','male'],'year':[2001,2001,2003,2002],'city':['北京','上海','廣州','北京']}df = pd.DataFrame(data,columns = ['name','year','sex','city','address'],index = ['a','b','c','d'])print(df)
name year sex city addressa 張三 2001 female 北京 NaNb 李四 2001 female 上海 NaNc 王五 2003 male 廣州 NaNd 小明 2002 male 北京 NaN

3.索引對象

Pandas的索引對象負責管理軸標簽和其他元數(shù)據(jù)(例如軸名稱等).構(gòu)建Series或DataFrame時,所用到的任何數(shù)組或其他序列的標簽都會被轉(zhuǎn)換成一個Index

例13.顯示DataFrame的索引和列

import pandas as pddata = {'name':['張三','李四','王五','小明'],'sex':['female','female','male','male'],'year':[2001,2001,2003,2002],'city':['北京','上海','廣州','北京']}df = pd.DataFrame(data,columns = ['name','year','sex','city','address'],index = ['a','b','c','d'])print(df)print(df.index)print(df.columns)
name year sex city addressa 張三 2001 female 北京 NaNb 李四 2001 female 上海 NaNc 王五 2003 male 廣州 NaNd 小明 2002 male 北京 NaNIndex(['a', 'b', 'c', 'd'], dtype='object')Index(['name', 'year', 'sex', 'city', 'address'], dtype='object')

索引對象不能進行修改,否則會報錯。不可修改性非常重要,因為這樣才能使Index對象在多個數(shù)據(jù)結(jié)構(gòu)之間安全共享
除了長的像數(shù)組,Index的功能也類似于一個固定大小的集合
例14.DataFrame的Index

import pandas as pddata = {'name':['張三','李四','王五','小明'],'sex':['female','female','male','male'],'year':[2001,2001,2003,2002],'city':['北京','上海','廣州','北京']}df = pd.DataFrame(data,columns = ['name','year','sex','city','address'],index = ['a','b','c','d'])print('name'in df.columns)print('a'in df.index)
TrueTrue

每個索引都有一些方法和屬性,他們可用于設(shè)置邏輯并回答有關(guān)該索引所包含的數(shù)據(jù)的常見的問題。

文章圖片3

例15.插入索引值

import pandas as pddata = {'name':['張三','李四','王五','小明'],'sex':['female','female','male','male'],'year':[2001,2001,2003,2002],'city':['北京','上海','廣州','北京']}df = pd.DataFrame(data,columns = ['name','year','sex','city','address'],index = ['a','b','c','d'])df.index.insert(1,'w')
Index(['a', 'w', 'b', 'c', 'd'], dtype='object')

4.查看DataFrame的常用屬性

DataFrame的基礎(chǔ)屬性有value、index、columns、dtypes、ndim和shape,分別可以獲取DataFrame的元素、索引、列名、類型、維度和形狀。

例16.顯示DataFrame的屬性

import pandas as pddata = {'name':['張三','李四','王五','小明'],'sex':['female','female','male','male'],'year':[2001,2001,2003,2002],'city':['北京','上海','廣州','北京']}df = pd.DataFrame(data,columns = ['name','year','sex','city','address'],index = ['a','b','c','d'])print(df)print('信息表的所有值為:\n',df.values)print('信息表的所有列為:\n',df.columns)print('信息表的元素個數(shù):\n',df.size)print('信息表的維度:\n',df.ndim)print('信息表的形狀:\n',df.shape)
name year sex city addressa 張三 2001 female 北京 NaNb 李四 2001 female 上海 NaNc 王五 2003 male 廣州 NaNd 小明 2002 male 北京 NaN信息表的所有值為:[['張三' 2001 'female' '北京' nan]['李四' 2001 'female' '上海' nan]['王五' 2003 'male' '廣州' nan]['小明' 2002 'male' '北京' nan]]信息表的所有列為:Index(['name', 'year', 'sex', 'city', 'address'], dtype='object')信息表的元素個數(shù):20信息表的維度:2信息表的形狀:(4, 5)
文章圖片4

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多