機器學習教程之pandas(二)

2021-08-15 01:24:50 字數 3702 閱讀 2127

這節主要講pandas結構的第2個結構dataframe。我們先來了解下什麼是dataframe?

乙個datarame表示乙個**,類似電子**的資料結構,包含乙個經過排序的列表集,它們沒乙個都可以有不同的型別值(數字,字串,布林等等)。datarame有行和列的索引;它可以被看作是乙個series的字典(每個series共享乙個索引)。與其它你以前使用過的(如rdata.frame)類似datarame的結構相比,在dataframe裡的面向行和面向列的操作大致是對稱的。在底層,資料是作為乙個或多個二維陣列儲存的,而不是列表,字典,或其它一維的陣列集合。

現在看看下面的練習題!

from pandas import series,dataframe

import numpy as np

date=

obj=dataframe(date)

print(obj)#輸出乙個**

#colunms 必須是date字典裡面的索引

obj=dataframe(date,columns=)

print(obj)

# my her his

# 0 bob wif 1

# 1 mom jiu 31

# 2 sis pro 45

# 3 bot ww 6

# 4 chi ss 7

#如果你傳遞了乙個行,但不包括在 data 中,在結果中它會表示為na值:#設定他的索引

obj=dataframe(date,columns=,index=['one','two','three','four','five'])

print(obj)

# his my she her

# one 1 bob nan wif

# two 31 mom nan jiu

# three 45 sis nan pro

# four 6 bot nan ww

# five 7 chi nan ss

# print(obj.columns)

#那麼怎麼取obj裡面的一段呢?有下面2種方法

print(obj['my'])

# one bob

# two mom

# three sis

# four bot

# five chi

# name: my, dtype: object

print(obj.his)

# one 1

# two 31

# three 45

# four 6

# five 7

# name: his, dtype: int64

# 行也可以使用一些方法通過位置或名字來檢索,例如 ix 索引成員(field)

print(obj.ix['three'])

# his 45

# my sis

# she nan

# her pro

# name: three, dtype: object

# 列可以通過賦值來修改。例如,空的 『she』 列可以通過乙個純量或乙個陣列來賦值:

obj['she']=np.arange(5)

print(obj)

# she his my her

# one 0 1 bob wif

# two 1 31 mom jiu

# three 2 45 sis pro

# four 3 6 bot ww

# five 4 7 chi ss

#當然還可以這樣 用series賦值

val=series(data=['1','9','20'],index=['one','three','four'])

obj['she']=val

print(obj)

# her my his she

# one wif bob 1 1

# two jiu mom 31 nan

# three pro sis 45 9

# four ww bot 6 20

# five ss chi 7 nan

#如果對一條不存在的列進行賦值將會建立,相反 del也可以刪除

obj['me']='jiu'

print(obj)

# she his her my me

# one 1 1 wif bob jiu

# two nan 31 jiu mom jiu

# three 9 45 pro sis jiu

# four 20 6 ww bot jiu

# five nan 7 ss chi jiu

del obj['me']

print(obj)

# her my his she

# one wif bob 1 1

# two jiu mom 31 nan

# three pro sis 45 9

# four ww bot 6 20

# five ss chi 7 nan

#另一種通用的資料形式是乙個巢狀的字典的字典格式:

pop=,

'zl':}

obj=dataframe(pop)

print(obj)

# 它的外部鍵會被解釋為列索引,內部鍵會被解釋為行索引:

# gy zl

# 200 3.3 30.0

# 201 4.4 nan

# 202 5.0 44.0

# 當然,你也可以對結果轉置:

print(obj.t)

# 200 201 202

# gy 3.3 4.4 5.0

# zl 30.0 nan 44.0

#也可以自己固定索引

obj=dataframe(pop,index=[201,202,203])

print(obj)

# gy zl

# 201 4.4 nan

# 202 5.0 44.0

# 203 nan nan

# 像series一樣, values 屬性返回乙個包含在dataframe中的資料的二維ndarray:

print(obj.values)

# [[ 4.4 nan]

# [ 5. 44. ]

# [ nan nan]]

最後說一下dataframe支援的構造器!

機器學習之pandas

import pandas as pd a pd.read csv 檔案路徑 讀取檔案 a.head 顯示的條數 顯示前部分資料 a.tail 顯示的條數 顯示後部分資料 a.columns 輸出列 a.loc 序列號 輸出乙個樣本 a.columns.tolist 將列轉換成列表 c.endwit...

Python 機器學習 Pandas

import pandas pandas 資料預處理非常很好使用 檢視資料 pandas詳細說明 讀取.csv檔案 輸入絕對路徑,同檔案可以相對路徑 print type food info 資料型別 dataframe 有許多行列組成 每一行或列交series print food info.dt...

機器學習 Pandas基礎學習

pandas是為了解決資料分析任務而建立的,納入了大量的庫和標準資料模型,提供了高效地操作大型資料集所需的工具。對於pandas包,在python中常見的匯入方法如下 from pandas import series,dataframe import pandas as pd pandas中的資料...