柚子快報(bào)激活碼778899分享:筆記 Pandas入門
一.數(shù)據(jù)加載導(dǎo)入數(shù)據(jù)
1.安裝和導(dǎo)入numpy和pandas
!pip install numpy
!pip install pandas
import numpy?as np
import pandas as pd
2.使用相對(duì)路徑和絕對(duì)路徑載入數(shù)據(jù)
3.每1000行為一個(gè)數(shù)據(jù)模塊,逐塊讀取
格式:chunker=pd. read _ csv('train. csv', chunksize=1000)
for i in chunker:
print(i)
read _ csv與read _ table對(duì)比: csv得到的表格對(duì)齊,分隔符為逗號(hào),而table按每行輸出,上下對(duì)應(yīng)但不對(duì)齊,分隔符為制表符'\t'。可以通過(guò)在括號(hào)內(nèi)添加sep=',/sep='\t'相互轉(zhuǎn)化。
csv與tsv對(duì)比: tsv文件以制表符為分隔符,標(biāo)準(zhǔn)格式下,字段值之中不允許出現(xiàn)制表符。而csv文件以半角逗號(hào)為分隔符 (也可以是逗號(hào)、其他字符、字符串)。
4.將表頭改成中文,索引改為乘客ID
二.初步觀察
1.查看數(shù)據(jù)的基本信息(總列數(shù),非空值數(shù)量,數(shù)據(jù)類型等): df.info ()
2.查看前/后幾列數(shù)據(jù)?: df. head()/df. tail()
3.判斷數(shù)據(jù)是否為空: df. isnull()
三.保存數(shù)據(jù): df. to _ csv()
四.Pandas基礎(chǔ)
1.查看列名: 全部——df. columns
某A列——df['A']/df. A
2.更改表頭
法一: df=pd. read _ csv('train. csv', names=['表頭1','表頭2',....], index _ col=", header=0)
index _ col更改索引, header確定替換列名為第幾行。
法二:創(chuàng)建字典
column={'header1':'表頭1','header2':'表頭2',……}
df. rename(columns=column, inplace=True)(inplace=True時(shí), 直接在原始數(shù)據(jù)上進(jìn)行重命名操作;
inplace=False時(shí),返回重命名后新的DateFrame, 原始數(shù)據(jù)不變)
3.刪除/隱藏某列: 刪除: del
df['A']/df. drop(['A'], axis=1)
df=df. drop(['a'], axis=1)
(當(dāng)axis=0時(shí)代表行, 1代表列)
隱藏: df. drop(['列名'],axis=1)
對(duì)比: del直接刪除, drop需要再賦值才可(df=.drop..),否則并不改動(dòng)原數(shù)據(jù) (即隱藏),del若想實(shí)現(xiàn)“隱藏”,需要分別保存改動(dòng)前后的數(shù)據(jù)。
4.數(shù)據(jù)篩選
范圍篩選:df=df[(df[Age′]<50)8(df[Age′]>10)]篩選在10-50的數(shù)據(jù), 并修改。
顯示某行:
df. loc[[行數(shù)],['列名']]
df. iloc[[行數(shù)],['列數(shù)']]
對(duì)比: loc通過(guò)列名實(shí)現(xiàn), iloc通過(guò)索引位置實(shí)現(xiàn)篩選。
(若在排序、分組、刪除等操作后,要對(duì)修改后數(shù)據(jù)篩選, 注意使用reset _ index()重置索引。)
5.構(gòu)建frame:frame=pd. DataFrame(np. arange(12). reshape(3,4),(構(gòu)建從0-11, 3行4列的矩陣)
index=['1',0',2'], (行索引)
columns=['X','B','F','G']) (列索引)
對(duì)比數(shù)據(jù)結(jié)構(gòu)DataFrame和Series:
DataFrame (二維) 每列可以有不同的數(shù)據(jù)類型, Series (一維) 僅有一種數(shù)據(jù)類型。
6.數(shù)據(jù)排序:
根據(jù)索引排序:
frame. sort _ index(axis=1, asxending=False)
(axis=1為列, False為降序, 默認(rèn)=0,升序排序)
根據(jù)值排序:
frame. sort _ value(by='列名', ascending=") (不接受axis參數(shù))
7.decribe查看數(shù)據(jù)基本信息: df. describe()
count:樣本數(shù)據(jù)大小
mean:樣本數(shù)據(jù)的平均值
std:樣本數(shù)據(jù)的標(biāo)準(zhǔn)差
min:樣本數(shù)據(jù)的最小值
25%:樣本數(shù)據(jù)25%的時(shí)候的值
50%:樣本數(shù)據(jù)50%的時(shí)候的值
75%:樣本數(shù)據(jù)75%的時(shí)候的值
max:樣本數(shù)據(jù)的最大值
8.篩選極值:
法一: python的max()/min()函數(shù): max(表達(dá)式)法二: df. max()(若對(duì)象為DataFrame或Series)
?
柚子快報(bào)激活碼778899分享:筆記 Pandas入門
精彩文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。