Python數(shù)據(jù)分析與數(shù)據(jù)挖掘 第3章 Python數(shù)據(jù)組織結(jié)構(gòu)_第1頁
Python數(shù)據(jù)分析與數(shù)據(jù)挖掘 第3章 Python數(shù)據(jù)組織結(jié)構(gòu)_第2頁
Python數(shù)據(jù)分析與數(shù)據(jù)挖掘 第3章 Python數(shù)據(jù)組織結(jié)構(gòu)_第3頁
Python數(shù)據(jù)分析與數(shù)據(jù)挖掘 第3章 Python數(shù)據(jù)組織結(jié)構(gòu)_第4頁
Python數(shù)據(jù)分析與數(shù)據(jù)挖掘 第3章 Python數(shù)據(jù)組織結(jié)構(gòu)_第5頁
已閱讀5頁,還剩127頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第3章Python數(shù)據(jù)組織結(jié)構(gòu)Python數(shù)據(jù)分析與數(shù)據(jù)挖掘3.1字符串創(chuàng)建和使用字符串字符串變量的創(chuàng)建方法是直接將字符串常量賦值給一個(gè)變量。>>>

str

=

"Hello

world">>>

str[0],

str[1],

str[-2],

str[3:7]("H",

"e",

"l",

"lo

w")>>>

str

=

"Hello

world">>>

strstr

=

"Hello

world"for

c

in

str:print(c,

end="")Hello

world"Hello

world"字符串格式化格式化操作符>>>tup=("Hello","world!")#元組>>>

"%s,

%s"

%tup>>>dict={"h":"Hello","w":"World!"}#字典>>>

"%(h)s,

%(w)s"

%

dict"Hello,

World!">>>

str

=

"Hello,

%s"

%"world!">>>

str>>>

"%s,

%s"

%

("Hello","world!")"Hello,

world!">>>

"%s,

%s"

%

"Hello","world!"TypeError:

not

enough

arguments

forformat

string"Hello,

world!"字符串格式化控制輸出寬度、精度from

math

import

piprint("1234567890")print("%.2f"

%

pi)print("%10f"

%

pi)print("%10.2f"

%

pi)#標(biāo)尺#精度2#字段寬10#字段寬10,精度212345678903.143.1415933.14字符串格式化模板字符串from

string

importTemplatestrTemp

=

Template("$x,

$y!")str

=strTemp.substitute(x="Hello",y="world")

print(str)s1

=

"Good

morning"s2

=

"Beijing"str

=

strTemp.substitute(x=s1,

y=s2)print(str)Hello,

world!Good

morning,

Beijing!字符串格式化模板字符串from

string

importTemplatestr1=Template("Hello,

w${x}d!")str1=str1.substitute(x="orl")print(str1)Hello,

world!from

string

importTemplated={"h":"Hello","w":"world"}str1=Template("$h,$w!")str1=str1.substitute(d)print(str1)Hello,world!字符串輸出格式化函數(shù)具體說明str.ljust(width,[fillchar])左對(duì)齊輸出字符串str,總寬度為參數(shù)width,不足部分以參數(shù)fillchar指定的字符填充,默認(rèn)使用空格填充。str.rjust(width,[fillchar])右對(duì)齊輸出字符串str,用法同上。str.center(width,[fillchar])居中對(duì)齊輸出字符串str,用法同上。str.zfill(width)將字符串str長(zhǎng)度變成width,右對(duì)齊,多余部分用0補(bǔ)足。字符串輸出格式化print("123456789012345678901234567890")#標(biāo)尺str

=

"Hello,world!"print(str.ljust(30))

#左對(duì)齊print(str.rjust(30,"_"))print(str.center(30,

"."))#右對(duì)齊

#居中對(duì)齊print(str.zfill(30))

#右對(duì)齊,補(bǔ)0123456789012345678901234567890Hello,

world!

Hello,

world!........Hello,

world!.........00000000000000000Hello,

world!字符串判斷函數(shù)具體說明str.startswith(substr)判斷str是否以substr開頭str.endswith(substr)判斷str是否以substr為結(jié)尾str.isalnum()判斷str是否全為字母或數(shù)字str.isalpha()判斷str是否全為字母str.isdigit()判斷str是否全為數(shù)字str.islower()判斷str是否全為小寫字母str.isupper()判斷str是否全為大寫字母轉(zhuǎn)換大小寫函數(shù)具體說明str.capitalize()將字符串str的首字母轉(zhuǎn)為大寫(注意是指整個(gè)字符串的首字母轉(zhuǎn)大寫,而非每個(gè)單詞的首字母轉(zhuǎn)大寫)。str.title()將字符串str中的每個(gè)單詞首字母轉(zhuǎn)為大寫,其余為小寫。str.upper()將字符串中的所有字母轉(zhuǎn)為大寫。str.lower()將字符串中的所有字母轉(zhuǎn)為小寫。str.swapcase()將字符串中的字母大小寫互換轉(zhuǎn)換大小寫>>>

str

=

"stuDENTSsTUDY">>>str.capitalize()#字符串首字母轉(zhuǎn)大寫,其余小寫"Students

study">>>str.title()#將字符串中的每個(gè)單詞首字母大寫,其余為小寫"Students

Study"其他處理對(duì)一個(gè)字符串以某分隔符進(jìn)行分割,分割后的結(jié)果生成一個(gè)列表str.split(sep=None,

maxsplit=-1)參數(shù)sep為分割符,默認(rèn)值為None,即按空格進(jìn)行分割;參數(shù)maxsplit定義最多進(jìn)行幾項(xiàng)分割。>>>

str

=

"

out

ofdateinformation

is

of

no

value

to

me.

">>>

str.split()["out",

"of",

"date",

"information",

"is",

"of",

"no",

"value",

"to",

"me."]>>>

str.split("

")["",

"out",

"of",

"date",

"information",

"is",

"of",

"no",

"value",

"to",

"me.",

"",

""]其他處理>>>slist=str.split(maxsplit=2)#分割str,得到字符串列表slist>>>

slist["out",

"of",

"date

information

is

of

no

value

to

me."]對(duì)一個(gè)字符串以某分隔符進(jìn)行分割,分割后的結(jié)果生成一個(gè)列表str.split(sep=None,

maxsplit=-1)其他處理>>>"-".join(slist)#將字符串列表slist中的各字符串拼接"out-of-date

information

is

of

no

value

to

me.

"將一個(gè)字符串序列拼接為一整個(gè)字符串str.join(iterable,

/)參數(shù)iterable為可迭代序列。>>>

slist["out",

"of",

"date

information

is

of

no

value

to

me."]其他處理生成一個(gè)將字符表示為字節(jié)編碼的字符串。bytes()bStr=bytes("我愛學(xué)Python!",encoding="utf-8")print(bStr)for

b

in

bStr:print(hex(b),

end="

")b"\xe6\x88\x91\xe7\x88\xb1\xe5\xad\xa6Python!"0xe6

0x88

0x91

0xe7

0x88

0xb1

0xe5

0xad

0xa6

0x50

0x79

0x74

0x68

0x6f

0x6e

0x21輸出特殊字符輸出(%)這個(gè)特殊字符>>>

str

=

"%s%%"

%

100>>>

str"100%"輸出($)符號(hào),可以使用$$符輸出$from

string

importTemplatestr1=Template("$$$x")str1=str1.substitute(x="100")print(str1)$1003.2列表創(chuàng)建列表可以通過列舉列表元素的方法創(chuàng)建列表,列表中的元素的類型可以是Python所支持的各種數(shù)據(jù)類型,并可以混合列舉組成列表。>>>

list4

=

[3,

"abc",

True]>>>

list4[3,

"abc",

True]>>>

list1=["hello","world"]>>>

list1>>>

list5

=

[123,

"hello",

[3,

"abc",

True]]>>>

list5[123,

"hello",

[3,

"abc",

True]]["hello",

"world"]創(chuàng)建列表利用list類的構(gòu)造函數(shù)list(),創(chuàng)建一個(gè)空列表,或?qū)⒍喾N其他類型的數(shù)據(jù)對(duì)象轉(zhuǎn)換為列表。#或lst=[]>>>

lst

=

list()>>>

lst#查看變量lst的數(shù)據(jù)類型>>>

type(lst)<class

"list">[]創(chuàng)建列表利用list類的構(gòu)造函數(shù)list(),創(chuàng)建一個(gè)空列表,或?qū)⒍喾N其他類型的數(shù)據(jù)對(duì)象轉(zhuǎn)換為列表。>>>

list(range(10,

15))[10,

11,

12,

13,

14]>>>list(("P","y","t","h","o","n"))#將元組轉(zhuǎn)換為列表["P",

"y",

"t",

"h",

"o",

"n"]>>>

list("Python")["P",

"y",

"t",

"h",

"o",

"n"]訪問列表list[index]list[start:end]list[index1][index2]#獲取一維列表中的索引值為index的元素

#獲取列表中從第start到end-1個(gè)元素#獲取二維列表中索引值為index1行index2列的元素>>>

lst

=

["coke",

"sugar",

"beer",

"apple"]>>>lst[0];lst[3]#訪問列表索引為0、3的元素"coke"["apple",

"orange"]>>>lst[0:3]#訪問列表索引為0到2的元素["coke",

"sugar",

"beer"]1.訪問列表元素訪問列表list[index]list[start:end]list[index1][index2]#獲取一維列表中的索引值為index的元素

#獲取列表中從第start到end-1個(gè)元素#獲取二維列表中索引值為index1行index2列的元素>>>lst_=[lst[:3],lst[1:]]#以獲取的子列表為元素構(gòu)成一個(gè)新的列表>>>

lst_;

len(lst_)[["coke",

"sugar",

"beer"],

["sugar",

"beer",

["apple",

"orange"]]]2>>>

lst_[1];

lst_[1][2]["sugar",

"beer",

["apple",

"orange"]]["apple",

"orange"]1.訪問列表元素訪問列表len(["coke","sugar","beer",["apple","orange"]])#共有4個(gè)元素42.獲取列表長(zhǎng)度訪問列表>>>

lst

=

["coke",

"sugar",

"beer",

"apple"]>>>max(lst);min(lst)

#字符串,按元素字母序依次比較大小"sugar""apple"3.返回列表元素最大值、最小值訪問列表4.定位列表元素list.index(value,

start=0,

stop=9223372036854775807,/)>>>

lst

=

["coke",

"sugar",

"beer",

"apple"]>>>

if

"beer"

in

lst:lst.index("beer")2>>>

lst.index("nay")Traceback

(most

recent

call

last):File

"<pyshell#21>",

line

1,

in

<module>lst.index("nay")ValueError:

"nay"

is

not

in

list訪問列表>>>

lst

=

["coke",

"beer",

"sugar",

"beer",

"apple",

"beer"]>>>

lst.count("beer")35.統(tǒng)計(jì)某個(gè)元素在列表中出現(xiàn)的次數(shù)訪問列表#列表lst原長(zhǎng)度為

4#添加的新元素是一個(gè)列表>>>

lst

=

["coke",

"sugar",

"beer",

"apple"]>>>

lst.append(["pork",

"beef"])>>>

lst;

len(lst)["coke",

"sugar",

"beer",

"apple",

["pork",

"beef"]]56.添加列表元素list.append(obj)訪問列表7.插入列表元素list.insert(pos,

new_value)>>>

lst

=

["coke","sugar","beer","apple"]>>>

lst.insert(2,

"pork")>>>

lst["coke",

"sugar",

"pork",

"beer",

"apple"]訪問列表>>>lst

#移除后["coke",

"beer",

"apple",

"pork"]8.移除列表元素list.pop([index])>>>

lst

=

["coke",

"sugar",

"beer",

"apple",

"pork"]>>>lst.pop(1)

#移除列表中索引為1的元素"sugar"訪問列表8.移除列表元素del

List

[index]>>>

lst

=

["coke",

"sugar",

"beer",

"apple",

"pork"]>>>

del

lst[1]>>>

lst;

len(lst)["coke",

"beer",

"apple",

"pork"]4訪問列表>>>

lst

=

["apple",

"banana",

"pear",

"grape",

"pork"]>>>lst.sort()

#升序排序>>>lst.sort(reverse=True)

#降序排序9.列表排序list.sort([key=None],

[reverse=False])參數(shù)key指定排序的回調(diào)函數(shù);參數(shù)reverse指定是否反序排序。訪問列表9.列表排序list.sort([key=None],

[reverse=False])>>>def

get2nd(n):

#定義排序方法函數(shù)return-n[1],n[0]>>>

lst

=

[[1,3],[4,3],[2,3],[1,5],[2,1]]>>>lst.sort(key=get2nd)

#使用get2nd()所定義的排序方法>>>

lst[[1,

5],

[1,

3],

[2,

3],

[4,

3],

[2,

1]]其中,get2nd()函數(shù)先后返回列表元素的第1個(gè)值的負(fù)數(shù)和第0個(gè)值,即令.sort()方法先按照第一個(gè)值進(jìn)行逆序排序,在此基礎(chǔ)上再按第二個(gè)值順序排序。訪問列表注意,reverse()僅僅是將列表元素按原順序的逆序進(jìn)行排列,并非按照內(nèi)容的大小進(jìn)行排序。10.反序列表list.reverse()>>>

lst

=

["apple",

"banana",

"pear",

"grape",

"pork"]>>>lst.reverse()

#反轉(zhuǎn)排列列表中的元素>>>

lst["pork",

"grape",

"pear",

"banana",

"apple"]訪問列表#添加到list1列表中>>>

list1.extend(list2)>>>

list1["coke",

"sugar",

"beer",

"apple",

"pork"]11.合并列表(擴(kuò)展列表元素)list.extend(list_)>>>

list1

=

["coke","sugar"]>>>

list2

=

["beer","apple","pork"]>>>list1+list2

#產(chǎn)生新的列表["coke",

"sugar",

"beer",

"apple",

"pork"]訪問列表>>>

s1.append(4)>>>

s1;

s2#s1添加一個(gè)元素,s1的內(nèi)容變?yōu)閇1,2,3,4]#s2也隨s1相應(yīng)改變了[1,

2,

3,

4][1,

2,

3,

4]12.復(fù)制列表list.copy()>>>

s1

=

[1,

2,

3]>>>

s2

=

s1.copy()>>>

s2[1,

2,

3]訪問列表12.復(fù)制列表list.copy()要獲得一個(gè)列表的拷貝,必須使用.copy()的方法,如果使用賦值的方法,得到的僅僅是類似指向原列表的一個(gè)指針。>>>

s1

=

[1,

2,

3]>>>s2=s1

#這時(shí)s2中內(nèi)容為[1,2,3]>>>s1.append(4)

#s1添加一個(gè)元素,s1的內(nèi)容變?yōu)閇1,2,3,4]>>>s1;s2

#s2也隨s1相應(yīng)改變了[1,

2,

3,

4][1,

2,

3,

4]對(duì)于一個(gè)可變的可迭代對(duì)象(如下面要介紹的集合set、映射dict),均必須使用.copy()的方法獲得該對(duì)象的拷貝。遍歷列表1.遍歷列表for…in…>>>

list1

=

["coke",

"sugar",

"beer",

"apple",

"pork"]>>>for

i

in

list1:

#遍歷列表元素print(i,end="")#不換行輸出列表元素coke

sugar

beer

apple

pork遍歷列表2.遍歷列表enumerate()for

index,

>>>

list1

=

["coke",

"sugar",

"beer",

"apple",

"pork"]>>>

for

idx,

val

in

enumerate(list1):

#遍歷列表元素print(idx,

val)

#輸出列表元素索引號(hào)和值value

in

enumerate(list):cokesugarbeerapplepork3.3元組創(chuàng)建元組#或tup=(1,2,3)>>>

tup

=

1,

2,

3>>>

tup#定義元素個(gè)數(shù)為1的元組,必須加逗號(hào)",",以區(qū)別加了括號(hào)的數(shù)字>>>

tup

=

(1,)>>>

tup(1,)(1,

2,

3)1.列舉元素創(chuàng)建元組2.

tuple()>>>tuple([3,2,1])#將列表轉(zhuǎn)換為元組(3,

2,

1)>>>tuple("work")#將字符串轉(zhuǎn)換為單個(gè)字母構(gòu)成的元組("w",

"o",

"r",

"k")>>>tuple((1,2,3))#列舉元素創(chuàng)建元組。注意不能使用tuple(1,2,3)創(chuàng)建元組(1,

2,

3)訪問元組>>>

tup

=

("apple",

"coke",

"orange",

"turkey",

"bean",

"carrot")>>>

lst

=

list(tup)>>>

lst.sort()>>>

tup

=

tuple(lst)>>>

tup("apple",

"bean",

"carrot",

"coke",

"orange",

"turkey")3.4集合創(chuàng)建集合>>>

set(["jeff",

"wong",

"cnblogs"])#字符串集合,排列順序是隨機(jī)的{"cnblogs",

"jeff",

"wong"}>>>set([5,4,3,3,2,2,1,1,0])#轉(zhuǎn)換為集合時(shí)會(huì)消除重復(fù)元素{0,

1,

2,

3,

4,

5}>>>

set(range(10)){0,

1,

2,

3,

4,

5,

6,

7,

8,

9}創(chuàng)建集合>>>

frozenset(range(10))frozenset({0,

1,

2,

3,

4,

5,

6,

7,

8,

9})使用frozenset(object)可以創(chuàng)建不可變空集合,或由其他可迭代序列對(duì)象(如列表、元組等)轉(zhuǎn)換創(chuàng)建不可變集合。>>>frozenset("python")

#由字符串所創(chuàng)建的集合,排列順序是隨機(jī)的frozenset({"t",

"n",

"y",

"p",

"h",

"o"})訪問集合獲取集合的長(zhǎng)度(使用len()函數(shù))訪問集合元素(不支持按索引訪問元素,只能通過遍歷集合元素進(jìn)行訪問)判斷集合是否存在元素>>>s=

set([1,

2,

3])>>>

2

in

s,

5

in

s(True,

False)訪問集合#[4,5,6]首先被轉(zhuǎn)化為集合{4,5,6},再添加>>>s

=

set([1,2,3])>>>

s.update([4,5,6])>>>

s{1,

2,

3,

4,

5,

6}4.添加集合元素>>s=

set("python")>>>

s.add(0)>>>

s{0,

"t",

"n",

"y",

"p",

"h",

"o"}訪問集合5.刪除集合元素set.remove()set.clear()>>>

s

=

set([1,

2,

3])>>>

s.remove(1)>>>

s{2,

3}>>>s

=

set(["a",

"b",

"c",

"d",

"e"])>>>s.remove("z")

#刪除不存在的元素Traceback

(most

recent

call

last):File

"<pyshell#191>",line

1,in

<module>s.remove("z")

#刪除不存在的元素KeyError:

"z"訪問集合6.子集、超集及集合關(guān)系操作符/判定函數(shù)示例說明==S1==S2判定S1是否等于S2,返回True/False。!=S1!=S2判定S1是否不等于S2,返回True/False。<S1<S2判定S1是否為S2的真子集,返回True/False。<=S1<=S2判定S1是否為S2的子集,返回True/False。>S1>S2判定S1是否為S2的真超集,返回True/False。>=S1>=S2判定S1是否為S2的超集,返回True/False。.issubset()S1.issubset(S2)判定S1是否為S2的子集或真子集,返回True/False。.issuperset()S1.issuperset(S2)判定S1是否為S2的超集或真超集,返回True/False。.isdisjoint()S1.isdisjoint(S2)判定S1是否與S2不相交,返回True/False。集合的運(yùn)算1.集合的交集集合的交集由所有既屬于集合A又屬于集合B的元素組成。使用intersection()方法計(jì)算兩個(gè)集合的交集。ersection()或“&”s

=

s1

&

s2s

=

ersection(s2)>>>

s1

=

set([1,

2,

3])>>>

s2

=

set([3,

4])>>>s1

&

s2#或s=ersection(s2){3}集合的運(yùn)算2.集合的并集set.union()

“|”>>>

s1=set([1,

2,

3])>>>

s2=set([2,

3,

4])>>>

s1.union(s2){1,

2,

3,

4}集合的運(yùn)算3.集合的差集集合的差集由所有屬于集合A但不屬于集合B的元素組成。使用difference()方法計(jì)算兩個(gè)集合的差集。set.difference()

或“-”s

=

s1

-

s2s

=

s1.difference(s2)>>>

s1

=

set([1,

2,

3])>>>

s2

=

set([3,

4])>>>s1-s2#或s=s1.difference(s2){1,

2}集合的運(yùn)算3.5映射(字典)定義字典>>>d={"name":"小明","sex":"男","age":18,"score":80}#構(gòu)成鍵值對(duì)兒>>>

d{"name":"小明","sex":"男","age":18,"score":80}1.賦值定義鍵key值valuename小明sex男age18score80定義字典>>>d=dict(name="小明",sex="男",age=18,score=80)#用"="構(gòu)成鍵值對(duì)兒,或者:>>>d=dict([("name","小明"),("sex","男"),("age",18),("score",80)])>>>

d{"name":"小明","sex":"男","age":18,"score":80}2.

dict(object)鍵key值valuename小明sex男age18score80定義字典>>>

d

=

{}>>>d["name"]="小明">>>

d[("model",

"cylinder",

"hpower")]

=

["Toyota",

4,

500]>>>

d{"name":"小明",("model","cylinder","hpower"):["Toyota",4,500]}>>>

d[set([123])]

=

"abc"Traceback

(most

recent

call

last):File

"<pyshell#14>",

line

1,

in

<module>d[set([123])]

=

"abc"TypeError:

unhashable

type:

"set"3.鍵的類型鍵key值valuename小明sex男age18score80定義字典4.字典嵌套字典項(xiàng)的值可以為Python定義的多種類型對(duì)象,甚至也可以是一個(gè)字典。d={"name":{"first":"Johney","last":"Lee"},"age":40}訪問字典>>>

d["age"]#取得age值40獲取字典長(zhǎng)度(len()函數(shù))訪問字典元素>>>

d

=

{"name":{"first":"Johney",

"last":"Lee"},

"age":40}>>>d["name"]["last"]#d["name"]取得{"first":"Johney","last":"Lee"},進(jìn)而取得其中鍵"last"的值"Lee""Lee"訪問字典3.添加字典元素>>>d={"name":"小明","sex":"男","age":18}>>>

d["score"]

=

80>>>

d["age"]

=

d["age"]

+

1>>>

d{"name":"小明","sex":"男","age":19,"score":80}訪問字典合并2個(gè)字典(update()函數(shù))判斷字典是否存在元素(關(guān)鍵字in,查找的是字典的鍵(key))刪除字典元素dict.pop(key[,default])>>>d={"age":18,"name":"小明","score":80,"sex":"男"}>>>d.pop("score")

#彈出以"score"為鍵的鍵值對(duì)兒,并返回對(duì)應(yīng)的值80>>>

d{"age":18,"name":"小明","sex":"男"}>>>d.pop("score","not

exists")#嘗試彈出鍵為"score"的元素,不存在,返回默認(rèn)值"not

exists"訪問字典7.遍歷字典元素for

key

in

Dict.keys():

#遍歷字典的鍵,即可使用Dict[key]獲得對(duì)應(yīng)的valuefor

value

in

Dict.values():

#遍歷字典的值,隨后可使用各value值for

key,value

in

Dict.items():#遍歷字典的鍵與值,隨后可使用key-value對(duì)d

=

{"age":

18,

"name":

"小明",

"score":

80,"sex":

"男"}for

key

in

d.keys():

#通過遍歷key,訪問所有的鍵值對(duì)兒print(key,

d[key])for

value

in

d.values():

#僅遍歷valueprint(value)for

key,value

in

d.items():

#遍歷key和valueprint(key,value)訪問字典8.清空字典dict.clear()3.6數(shù)組ndarray創(chuàng)建數(shù)組numpy.array(object,

dtype=None,

copy=True,

order="K",

subok=False,ndmin=0)>>>

t1

=

(1,

2,

3);

t2

=

(2,

3,

4)>>>

arr

=

np.array((t1,

t2))>>>

arrarray([[1,

2,

3],[2,

3,

4]])>>>

arr1

=

np.array((5,

4,

3,

2,

1),dtype=

"float64")>>>

arr1>>>arr3=np.array((5,4,3,2,1),ndmin=3)

#創(chuàng)建3維數(shù)組>>>

arr3array([[[5,

4,

3,

2,

1]]])>>>

arr

=

np.array([[1,

2,

3],

[4,

5,

6],[7,

8,

9]])>>>

arrarray([[1,

2,

3],[4,

5,

6],[7,

8,

9]])array([5.,

4.,

3.,

2.,

1.])訪問數(shù)組#一維數(shù)組>>>

arr1

=

np.array([3,

6])>>>

arr1.ndim;

arr1.shape1(2,)>>>arr2=np.array([[[1],[2]],[[3],[4]],[[5],[6]]])#三維數(shù)組>>>arr2.ndim;arr2.shape

#可得維數(shù)為3,各維階數(shù)為(3,2,1)3(3,

2,

1)1.維數(shù)和各維階數(shù)ndarray.ndimndarray.shape訪問數(shù)組#生成一個(gè)由隨機(jī)數(shù)數(shù)組>>>

arr

=

np.array(np.random.rand(2,

2))>>>

arr>>>arr.dtype#查看數(shù)組元素的數(shù)據(jù)類型dtype("float64")array([[0.9215393

,

0.43412927],[0.94762942,

0.21441442]])2.元素類型和個(gè)數(shù)ndarray.ndimndarray.shape>>>

arr.size4訪問數(shù)組>>>

arr.reshape((2,

3))#將數(shù)組變換為2行3列array([[1,

2,

3],[4,

5,

6]])3.改變數(shù)組維數(shù)和階數(shù)ndarray.reshape(shape,order="C")>>>

arr

=

np.array([[[1,

2]],

[[3,

4]],

[[5,

6]]])>>>

arr.shape(3,

1,

2)訪問數(shù)組>>>

arr.reshape(-1,)#變換為一維數(shù)組。參數(shù)-1表示結(jié)果由數(shù)據(jù)個(gè)數(shù)確定>>>

arr.reshape(-1,

1)#變換為n行1列,這里用-1表示結(jié)果由數(shù)據(jù)個(gè)數(shù)確定array([[1],[2],[3],[4],[5],[6]])array([1,

2,

3,

4,

5,

6])(3,

1,

2)3.改變數(shù)組維數(shù)和階數(shù)ndarray.reshape(shape,order="C")訪問數(shù)組#數(shù)據(jù)類型變?yōu)榱?float64">>>

arr1;arr1.dtypearray([[[1.,

2.]],[[3.,

4.]],[[5.,

6.]]])dtype("float64")4.更改數(shù)據(jù)類型ndarray.astype()>>>

arr

=

np.array([[[1,

2]],

[[3,

4]],

[[5,

6]]],

dtype="str")>>>

arrarray([[["1",

"2"]],[["3",

"4"]],[["5",

"6"]]],

dtype="<U1")>>>arr1=arr.astype(float)

#或arr1=arr.astype("float32")等訪問數(shù)組>>>

arr1

=

np.array([[2,

4],

[5,

7],

[8,

1]])>>>

arr1[0]#二維數(shù)組#得到一維數(shù)組,為原二維數(shù)組的第0行>>>

arr1[0:2,

:]#仍得到二維數(shù)組,保留了原有的維度結(jié)構(gòu)array([[2,

4],[5,

7]])array([2,

4])5.訪問數(shù)組數(shù)據(jù)>>>

arr1[0,

:]#得到一維數(shù)組,為原二維數(shù)組第0行的各列array([2,

4])數(shù)組的運(yùn)算array([[1,

2],[3,

4],[5,

6]])1.數(shù)值統(tǒng)計(jì)ndarray.sum(axis=None,

dtype=None,

out=None,

keepdims=False,

initial=0,

where=True)ndarray.mean(axis=None,

dtype=None,

out=None,

keepdims=False)ndarray.max(axis=None,

out=None,

keepdims=False,

initial=<no

value>,

where=True)ndarray.min(axis=None,

out=None,

keepdims=False,

initial=<no

value>,

where=True)>>>arr

>>>arr.sum()#或np.sum(arr)21>>>arr.sum(axis=0)#或

np.sum(arr,axis=0),按列求和array([

9,

12])#或np.sum(arr,>>>arr.sum(axis=1)axis=1),按行求和array([

3,

7,

11])數(shù)組的運(yùn)算1.數(shù)值統(tǒng)計(jì)ndarray.sum(axis=None,

dtype=None,

out=None,

keepdims=False,

initial=0,

where=True)ndarray.mean(axis=None,

dtype=None,

out=None,

keepdims=False)ndarray.max(axis=None,

out=None,

keepdims=False,

initial=<no

value>,

where=True)ndarray.min(axis=None,

out=None,

keepdims=False,

initial=<no

value>,

where=True)>>>

arrarray([[1,

1,

4],[2,

3,

7],[4,

2,

9]])>>>np.argmax(arr,0)#計(jì)算所有列的最大值對(duì)應(yīng)在該列中的索引array([2,

1,

2],

dtype=int64)>>>np.argmin(arr[1,:])#計(jì)算第2行中最小值所在列的索引0數(shù)組的運(yùn)算>>>

arr1**2array([16,

4,

25],

dtype=int32)>>>

1/arr1array([0.25,

0.5

,

0.2

])2.加、減、乘、除、平方、倒數(shù)、負(fù)數(shù)、異或>>>

arr1

=

np.array([4,

2,

5])>>>

arr2

=

np.array([2,

4,

5])>>>

arr1

+arr2array([

6,

6,

10])>>>

-arr1array([-4,

-2,

-5])>>>

arr1

*

arr2array([

8,

8,

25])數(shù)組的運(yùn)算>>>a+1

#運(yùn)算時(shí)1進(jìn)行了擴(kuò)充np.array([[1,1],[1,1],[1,1]])array([[2.,

3.],[4.,

5.],[6.,

7.]])>>>a/b#運(yùn)算時(shí)數(shù)組b進(jìn)行了擴(kuò)充np.array([[1.,2.],[1.,2.],[1.,2.]])array([[1.,

1.],[3.,

2.],[5.,

3.]])如果兩個(gè)數(shù)據(jù)維度不一致時(shí),低維數(shù)據(jù)會(huì)自動(dòng)進(jìn)行維度的復(fù)制擴(kuò)充,進(jìn)而完成相應(yīng)的運(yùn)算。>>>x=np.array([1,2,3])

#一維數(shù)組,shape=(3,)>>>w=np.array([[1,2,3],[4,5,6]])

#二維數(shù)組,shape=(2,3)>>>

w

*

xarray([[

1,

4,

9],[

4,

10,

18]])>>>a=np.array([[1.,2.],[3.,4.],[5.,6.]])

#二維數(shù)組,各維階數(shù)為(3,2)>>>b=np.array([1.,2.])#一維數(shù)組,各維階數(shù)(2,)數(shù)組的運(yùn)算如果兩個(gè)數(shù)據(jù)維度不一致時(shí),低維數(shù)據(jù)會(huì)自動(dòng)進(jìn)行維度的復(fù)制擴(kuò)充,進(jìn)而完成相應(yīng)的運(yùn)算。>>>a=np.array([[1.,2.],[3.,4.],[5.,6.]])

#二維數(shù)組,各維階數(shù)為(3,2)>>>b=np.array([[1.],[2.],[3.]])

#二維數(shù)組,各維階數(shù)為(3,1)>>>

a

/

b#運(yùn)算時(shí)數(shù)組b進(jìn)行了擴(kuò)充:np.array([[1.,1.],[2.,2.],[3.,3.]])array([[1. ,

2.

],[1.5 ,

2.

],[1.66666667,

2.

]])數(shù)組的運(yùn)算>>>

arr1.shape;

arr1.T.shape#一維數(shù)組轉(zhuǎn)置后階數(shù)不變>>>

arr2.shape;

arr2.T.shape#二維數(shù)組轉(zhuǎn)置后,各維階數(shù)互換(1,

2)(2,

1)(2,)(2,)3.轉(zhuǎn)置ndarray.T數(shù)組的運(yùn)算4.指數(shù)運(yùn)算,以e為底numpy.exp(x)>>>

a

=

np.array([

2,

5,

7])>>>

np.exp(a)array([

7.3890561

,

148.4131591

,

1096.63315843])訪問數(shù)組>>>np.concatenate((a,b.T),axis=1)#沿行合并后,shape為(2,3)array([[1,

2,

5],[3,

4,

6]])點(diǎn)積、內(nèi)積、外積合并numpy.concatenate((a1,

a2,

...),

axis=0,

out=None)>>>a=np.array([[1,2],[3,4]])

#shape為(2,2)>>>b=np.array([[5,6]])

#shape為(1,2)>>>np.concatenate((a,b),axis=0)

#沿列合并后,shape為(3,2)array([[1,

2],[3,

4],[5,

6]])>>>np.concatenate((a,b),axis=None)#axis=None,合并為一維(6,)數(shù)組array([1,

2,

3,

4,

5,

6])訪問數(shù)組>>>

a

=

np.array([[1,2],

[3,4]])>>>

b

=

np.array([[5,6],

[7,8]])>>>np.vstack((a,b))#按列合并,增加行數(shù)>>>np.hstack((a,b))#按行合并,即行數(shù)不變,擴(kuò)展列數(shù)array([[1,

2,

5,

6],[3,

4,

7,

8]])array([[1,

2],[3,

4],[5,

6],[7,

8]])點(diǎn)積、內(nèi)積、外積合并numpy.vstack()或numpy.hstack()生成特定值ndarray對(duì)象numpy.zeros(shape,

dtype=float,

order="C")numpy.ones(shape,

dtype=None,

order="C")numpy.eye()numpy.diag()>>>np.diag([1,2,3])#對(duì)角數(shù)組array([[1,

0,

0],[0,

2,

0],[0,

0,

3]])>>>

np.zeros(3)>>>

np.ones((3,

3))array([[1.,

1.,

1.],[1.,

1.,

1.],[1.,

1.,

1.]])>>>np.eye(3)

#單位數(shù)組array([[1.,

0.,

0.],[0.,

1.,

0.],[0.,

0.,

1.]])array([0.,

0.,

0.])3.7矩陣matrix創(chuàng)建matrix對(duì)象numpy.matrix(data,

dtype=None,

copy=True)>>>

np.matrix([[1,2,3],[4,5,6],[7,8,9]],dtype=float)#由列表數(shù)據(jù)創(chuàng)建matrix([[1.,

2.,

3.],[4.,

5.,

6.],[7.,

8.,

9.]])>>>np.matrix(((1,2,3),(4,5,6)))#由元組數(shù)據(jù)創(chuàng)建矩陣>>>

np.matrix(np.array([[1,2,3],[4,5,6],[7,8,9]]))#由數(shù)組數(shù)據(jù)創(chuàng)建matrix([[1,

2,

3],[4,

5,

6],[7,

8,

9]])>>>

np.matrix([np.array([1,2,3]),np.array([4,5,6])])#由數(shù)組數(shù)據(jù)創(chuàng)建matrix([[1,

2,

3],[4,

5,

6]])matrix([[1,

2,

3],[4,

5,

6]])訪問matrix>>>

mtx[1,2]#訪問第1行第2列元素7>>>

mtx>>>mtx[1]#訪問子矩陣,或mtx[1,:]matrix([[5,

6,

7,

8]])#訪問第0、1行、第1、2>>>

mtx[0:2,

1:3]列構(gòu)成的2x2矩陣matrix([[2,

3],[6,

7]])matrix([[

1,

2,

3,

4],[

5,

6,

7,

8],[

9,

10,

11,

12],[13,

14,

15,

16]])matrix運(yùn)算>>>

mtx.getT()#求轉(zhuǎn)置矩陣matrix([[1,

3],[2,

4]])>>>

mtx

=

np.matrix(([1,2],[3,4]))>>>mtx.I

#求逆矩陣matrix([[-2.

,

1.

],[

1.5,

-0.5]])數(shù)值統(tǒng)計(jì)與檢索加、減、除、倒數(shù)、負(fù)數(shù)、異或求逆和轉(zhuǎn)置numpy.matrix.I

numpy.matrix.getI()numpy.matrix.T

numpy.matrix.getT()matrix運(yùn)算>>>

np.multiply(a,

b)matrix([[2,

2]])4.矩陣相乘和點(diǎn)積numpy.multiply()numpy.dot()>>>

mtx1

=

np.matrix((1,2,3))#1x3矩陣>>>

mtx2

=

np.matrix([[1],[2],[3]])#3x1矩陣>>>mtx1

*

mtx2

#得到1x1矩陣matrix([[14]])>>>

a

=

np.matrix([1,1])>>>

b

=

np.matrix([2,2])>>>

b

=

np.matrix([2,2])>>>

b

*

2matrix([[4,

4]])matrix運(yùn)算5.合并(與array相似)矩陣、列表、數(shù)組間的互相轉(zhuǎn)換matrix.getA()matrix.tolist()#創(chuàng)建一個(gè)矩陣#將矩陣轉(zhuǎn)換成列表>>>

matrix1.tolist()[[1,

2],

[3,

2],

[5,

2]]>>>

matrix1

=

np.matrix([[1,2],[3,2],[5,2]])>>>matrix1.getA()

#將矩陣轉(zhuǎn)換成數(shù)組array([[1,

2],[3,

2],[5,

2]])>>>arr1=np.array(matrix1)#將矩陣轉(zhuǎn)換成數(shù)組3.8系列Series創(chuàng)建Series對(duì)象pandas.Series(data=None,

index=None,

dtype=None,

name=None,

copy=False,fastpath=False)>>>pd.Series(data=[1,2,3,4,5],index=["a","b","c","d","e"],name="s",dtype=float)#指定index、name和dtype>>>

d={"a":1,"b":2,"c":3,"d":4,"e":5}>>>

s

=

pd.Series(d,

name="sDict",dtype=float)Name:

sDict,

dtype:

float64a

1.0a1.0b

2.0b2.0c

3.0c3.0d

4.0d4.0e

5.0e5.0Name:

s,

dtype:

float64訪問Series對(duì)象1.訪問對(duì)象結(jié)構(gòu)>>>#對(duì)于上例中的Series對(duì)象>>>="series"

#訪問和修改Series對(duì)象的name>>>s.index=["q","w","e","r","t"]

#訪問和修改index>>>

sq1.0w2.0e3.0r4.0t5.0Name:

series,

dtype:

float64訪問Series對(duì)象>>>s.values[2]=333#修改特定數(shù)據(jù)>>>

sName:

series,

dtype:

float642.訪問數(shù)據(jù)>>>s.values

#對(duì)于上例中的Series對(duì)象,訪問其數(shù)據(jù)array([1.,

2.,

3.,

4.,

5.])>>>

s.values[:]

=

[11,22,33,44,55]>>>

sq1.0a11.0w2.0b22.0e333.0c33.0r4.0d44.0t5.0e55.0Name:

series,

dtype:

float64Series對(duì)象的運(yùn)算>>>

s**3#或s.pow(3)a

1.0b

8.0c

27.0d

64.0e

125.0Name:

sDict,

dtype:

float64a

1.0b

4.0c

9.0d

16.0e

25.0Name:

sDict,

dtype:

float641.數(shù)學(xué)及統(tǒng)計(jì)運(yùn)算>>>#對(duì)于上例中的原始Series對(duì)象>>>s

*

11#與數(shù)值相乘,即為與每個(gè)Series的數(shù)據(jù)元素相乘a

11.0b

22.0c

33.0d

44.0e

55.0Name:

sDict,

dtype:

float64>>>s*(1,2,3,4,5)#與“數(shù)組”相乘,即為與每個(gè)Series的數(shù)據(jù)元素對(duì)應(yīng)相乘Series對(duì)象的運(yùn)算1.數(shù)學(xué)及統(tǒng)計(jì)運(yùn)算>>>s.count();s.sum()#計(jì)算計(jì)數(shù)、總和515.0>>>s.max();s.min()#計(jì)算最大、最小值>>>

s.mean()#計(jì)算均值3.0>>>s.std()

#計(jì)算標(biāo)準(zhǔn)差1.58113883008418985.01.0Series對(duì)象的運(yùn)算2.更改數(shù)據(jù)類型>>>

ser

=

pd.Series(["20210826",

"20210827",

"20210828",

"20210829"])>>>

ser0

202108261

202108272

202108283

20210829dtype:

objectSeries對(duì)象的運(yùn)算2.更改數(shù)據(jù)類型#轉(zhuǎn)換為datetime數(shù)據(jù)類型>>>

ser_dt

=

ser.apply(pd.to_datetime)>>>

ser_dt0

2021-08-261

2021-08-272

2021-08-283

2021-08-29dtype:

datetime64[ns]Series對(duì)象的運(yùn)算2.更改數(shù)據(jù)類型>>>ser_dt.map(lambda

x:str(x))#使用.map()將datetime型轉(zhuǎn)為str型0

2021-08-26

00:00:001

2021-08-27

00:00:002

2021-08-28

00:00:003

2021-08-29

00:00:00dtype:

object3.9數(shù)據(jù)框架DataFrame創(chuàng)建DataFrame對(duì)象pandas.DataFrame(data=None,

index=None,

columns=None,

dtype=None,copy=False)>>>df=

pd.DataFrame()>>>

dfEmpty

DataFrameColumns:

[]Index:

[]創(chuàng)建DataFrame對(duì)象函

數(shù)說

明DataFrame(由數(shù)組、列表或元組組成的字典)每個(gè)序列會(huì)變成DataFrame的一列。所有序列的長(zhǎng)度必須相同。DataFrame(由Series組成的字典)每個(gè)Series會(huì)成為一列。如果沒有顯式制定索引,則各

Series的索引會(huì)被合并成結(jié)果的行索引。DataFrame(由字典組成的字典)各內(nèi)層字典會(huì)成為一列。鍵會(huì)被合并成結(jié)果的行索引。DataFrame(二維ndarray)數(shù)據(jù)矩陣,還可以傳入行標(biāo)和列標(biāo)。DataFrame(字典或Series的列表)各項(xiàng)將會(huì)成為DataFrame的一行。索引的并集會(huì)成為

DataFrame的列標(biāo)。DataFrame(由列表或元組組成的列表)類似于二維ndarrayDataFrame(DataFrame)沿用DataFrame創(chuàng)建DataFrame對(duì)象>>>

d

=

{"height"

:

np.array([1,

2,

3,

4,

5]),"weight"

:

[5,

4,

3,

2,

1],"age":(15,14,16,17,13)}#定義多種類型數(shù)據(jù)構(gòu)成的字典>>>idx=("KEVIN","SAM","PETER","HELEN","SMITH")#類型為元組>>>df=pd.DataFrame(data=d,index=idx)#創(chuàng)建DataFrame>>>

dfheightweight

ageKEVIN

15

15SAM

24

14PETER

33

16HELEN

42

17SMITH

51

13創(chuàng)建DataFrame對(duì)象如果在創(chuàng)建DataFrame對(duì)象時(shí)不指定columns和index,則使用默認(rèn)序號(hào)值。>>>df=pd.DataFrame(np.random.randint(0,10,(4,5)))#產(chǎn)生隨機(jī)數(shù)>>>df

#未指定columns和index0

1

2

340

035361

790132

733493

43332訪問DataFrame>>>df.index=("KEVIN","SAM","PETER","HELEN","SMITH")#修改index內(nèi)容>>>df.index.rename("IDX",inplace=True)

#修改index的名字>>>df.columns=["code","number","value","level","score"]#修改columns內(nèi)容>>>df.columns.rename("COL",inplace=True)#修改columns名字>>>

dfe="object",

name="COL")COL code

number

value

level

scoreIDXKEVIN50337SAM93524PETER76881HELEN67781SMITH598941.訪問index和columns>>>

df0

1

2

3

40

5

0

3

3

71

9

3

5

2

42

7

6

8

8

13

6

7

7

8

14

5

9

8

9

4訪問Dat

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論